Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

why do I get this error? (scope?)

 
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
 
author & internet detective
Posts: 39586
781
Eclipse IDE VI Editor Java
  • 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
 
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?
 
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
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!