Reference variable to an object instantiated/initialized in another class in Java

Posted by Alex on Stack Overflow See other posts from Stack Overflow or by Alex
Published on 2011-01-11T20:44:10Z Indexed on 2011/01/11 20:53 UTC
Read the original article Hit count: 176

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about reference