my dog learned polymorphism*
The moose likes Beginning Java and the fly likes error: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "error: "cannot find symbol"" Watch "error: "cannot find symbol"" New topic
Author

error: "cannot find symbol"

MR Chen
Ranch Hand

Joined: Aug 10, 2007
Posts: 31
Hello All,

So, I've got this little piece of code:



When I compile, it gives me this error:



So, why does it accept the first reference to GameBoard, but not the second? I'm sure there's something fundamental I'm missing here...

Thanks,
Mike
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9048
    
  10
method whoIsThere "GameBoard" cannot see the "GameBoard" that is declared in the constructor.


would allow gameBoard to be visible from both the constructor and the whoIsThere method.

(Convention is that variable and method names should begin with lower case while class names begin with upper case.)


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
MR Chen
Ranch Hand

Joined: Aug 10, 2007
Posts: 31
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

outside of the constructor?

Again - many thanks,
Mike
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.
 
 
subject: error: "cannot find symbol"