Originally posted by MR Chen: ... I'm sure there's something fundamental I'm missing here...
Yes, it's an issue of scope, meaning where a variable is valid.
Your variable "GameBoard" is declared inside the body of the MyGame constructor, so its scope is limited to that constructor's body. Therefore, within the method whoIsThere, "GameBoard" is out of scope, and has no meaning.
I think you want to define this as an instance variable so that it's available everywhere in the object. If you wish, you can still value the variable in the constructor...
[ September 05, 2007: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Aug 10, 2007
Marilyn/marc - thank you for your quick replies. I'll have to keep this scope thing in mind...
I notice that both of you declared "gameBoard" outside of the constructor, but then valued it inside the constructor:
Is there any advantage to doing it that way, as opposed to just using
I did it that way to show that you can still value it from the constructor, if you wish. There are situations where that might be necessary -- for example, if the constructor is passed values that need to be used in constructing a GameBoard.
However, you can certainly initialize an instance variable at the point of declaration. Personally, I prefer this (when possible and practical) because it's a way of guarding against a null exception.