Reference variable to an object instantiated/initialized in another class in Java
- by Alex
The reason I'm asking is because I'm getting NullPointerException.
I now this is very easy but I'm pretty new programming and find this a bit confusing.
So say I have initialized an object in a class and want to access that same object from another class.
Like now for instance I'm working on a small Chess game, in my model Game class I have an instance of Board, an object. Board, in turn, has an array of Squares. Square[][].
Game has board, board has Square[][].
Now if I want to access the Square[][] through the object board (in Game) of type Board.
Do I just declare a variable with the same name and type or do I have to initialize it again?
Board board OR Board board = new Board();
Note, I have already initialized board in the class Game so if I do it again, won't they be two totally different Board objects?