This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why do I get this error? (scope?)  RSS feed

 
MR Chen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try to compile this code



I get this error:

cannot find symbol
symbol : variable barney
location : class Vacation
barneyFamily = barney.getFamily();

I've run into this "cannot find symbol" error before, and it's been a "scope" issue (previously, the variable was declared within a constructor, but that's not the case this time...)

Any thoughts?

Thanks,
Mike
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37242
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
barney is declared in the Fred class. The Vacation class is trying to reference barney. How does it know to look in the Fred class?
 
MR Chen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
barney is declared in the Fred class. The Vacation class is trying to reference barney. How does it know to look in the Fred class?


Hi Jeanne,

Hmmm. Good question. I guess I (mistakenly) thought that once the object was created and out on the heap, you could just reference it. I don't remember seeing anything about it in Head First Java ("scope" always seemed to refer to variables there, unless I missed it.)

On a hunch, I tried changing it to Fred.barney.getFamily(); but the compiler still barfed.

Another hint?

Mike
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by MR Chen:

Hmmm. Good question. I guess I (mistakenly) thought that once the object was created and out on the heap, you could just reference it. I don't remember seeing anything about it in Head First Java ("scope" always seemed to refer to variables there, unless I missed it.)

We are talking variables here. If vacation needs to play with a Friend object, perhaps it needs a Friend variable to hold the reference to the object? Also it needs a way to set that same Friend variable...
 
MR Chen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If vacation needs to play with a Friend object, perhaps it needs a Friend variable to hold the reference to the object? Also it needs a way to set that same Friend variable...


OK, so it sounds like my Vacation instance (trip) needs to have a Friend variable that I can use as a handle to get at the Friend (barney). I guess that makes sense (since I can't do what I thought I could - just walk over to the heap and ask it for what I want). But how do I get the barney info in there if I can't reference barney from trip?

Well, what if I do it from somewhere that I can reference both barney AND trip:



But, of course, this doesn't work - I get an "<identifier> expected" error on

trip.setLittleBuddy(barney);

Is that because I've botched the syntax somewhere, or is my whole idea just rubbish?
 
Srinivasan Mahadevan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Fred
{
Vacation trip = new Vacation();
Friend barney = new Friend();
trip.setLittleBuddy(barney);

public static void main (String[] args) {
new Fred();
}
}

Inside a class body, variables can be declared or methods can be declared. Method invocation can happen only within another method. You have made the method invocation call "trip.setLittleBuddy(barney);" in places where instance variables / static variables can be declared. Since the compiler does not expect method invocation in this place, you get the compiler error.
[ October 19, 2007: Message edited by: Srinivasan M ]
 
MR Chen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You have made the method invocation call "trip.setLittleBuddy(barney);" in places where instance variables / static variables can be declared.


Ah, yes, I'd forgotten that. What an incredibly unhelpful error message. Thanks for pointing out the issue.

So, now I've moved "trip.setLittleBuddy(barney);" into the constructor, and everything is fine.

Jeanne, pete, and Srinivasan - thanks for your help!

Mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!