aspose file tools*
The moose likes Beginning Java and the fly likes non-static var from static context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "non-static var from static context" Watch "non-static var from static context" New topic
Author

non-static var from static context

Al Olivera
Greenhorn

Joined: May 27, 2003
Posts: 3
Hello folks, I am getting this compile error "non-static variable this cannot be referenced from a static context".
I have this class:
public class Zoo {
String name; String location;
Zoo(String name, String location) {
this.name=name; this.location=location;
} }
and from main would like to create an instance of the object Zoo -- thusly:
Zoo SanDiego = new Zoo ("San Diego", "CA");
I guess I don't understand how this is a "static context" - can anyone please advise?
thanks! Great website!
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Al,
Welcome to JavaRanch. Umm, strange . That ought to work. Try posting the whole class so we can take a look at it. Be sure to use the code button and paste your code between the tags.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Amy Phillips
Ranch Hand

Joined: Apr 02, 2003
Posts: 280
I often have this problem and expect you have your main method as static. I usually solve it by just making the other method static as well.
I have however read Kathy's description of this and learnt that this is a big no no but to be honest have no idea how to get avoid it
Al Olivera
Greenhorn

Joined: May 27, 2003
Posts: 3
Hi again,
Here is a code snippet -- anyone ever see the game "Zoo Tycoon"? I'd like to use it as a template to practice, great ideas for classes, methods, etc.

Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Ah! You have Zoo as a non-static inner class. You can either declare Zoo in a seperate file or do this:

You must have an instance of the enclosing class to create a non-static inner class instance from a static context.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Al Olivera
I suggest you to using the overriding the default toString() method to display the result because if the instance variable (name and location) change to class variable,it will affect the situation which you want to create more than one object to store the independent value to instance variable.
If you change to class variable(static) you will share the variable to other instance(object),it means that you create two object of ZooTycoon,you will get the same thing(name and location).

I am not sure whether it is work or not
But I think Michael's method is better
Hope this help
[ June 05, 2003: Message edited by: siu chung man ]

Francis Siu
SCJP, MCDBA
Mike Nightsky
Ranch Hand

Joined: Aug 18, 2001
Posts: 48
do you really need a inner class that named like the outer class?
i mean its a little confusing.
and if its only to fix the static referenc problem there are other ways.
Mike Nightsky


Win the opportunity to make money on the Internet<br /><a href="http://sweeps.sitesell.com/minirich.html" target="_blank" rel="nofollow">http://sweeps.sitesell.com/minirich.html</a>
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Originally posted by Amy Phillips:
I often have this problem and expect you have your main method as static. I usually solve it by just making the other method static as well.
I have however read Kathy's description of this and learnt that this is a big no no but to be honest have no idea how to get avoid it

Howdy Amy!
*Everybody* has that problem in the beginning .
We tell our students to Get Out Of Main And Into ObjectLand as soon as possible using:

In other words, just don't do anything at all in main, except instantiate the class you're in, and you're good. Or, you can force yourself to do this by always making a *separate* class for your main like this:

There are advantages and disadvantages to each way -- creating a separate file as the "launcher" or putting the launcher code inside the class itself. It depends on how you like to organize things, and whether you always write tester code for your classes anyway.
But really, the important point is to LEAVE the static world behind at the earliest moment
And since you probably don't need the reference to Foo from within your static main, you can just shorten it to:
public static void main(String[] args) {
new Foo().go();
}
Which gets you going without having to assign the new foo object to a reference variable.
Now, for Al, I would not recommend using an inner class from a design perspective, because the Zoo class should probably be a separate, reusable class. In the beginning, I would think of using inner classes *only* under these situations:
1) The outer class needs a "helper" class, that can do work on the outer class's behalf, and that makes no sense to reuse. The classic example is an event handler. An event handler is almost impossible to reuse outside the enclosing class, because it is doing something very specific to THAT gui (controlling it's buttons, putting text into text areas, etc.)
And one reason for making the event handler an inner class is that using inner classes lets you have more than one implementation of the very same interface. For example, you CANNOT do this in Java:

You get only ONE chance to implement an interface. But an inner class (or more than one...) can let you work around this by having:

In these examples, we assume that the actionPerformed method needs access to the outer class's private instance variables (e.g. references to the GUI objects the outer class has built), so that's why you need to use an inner class.
The OTHER reason to use an inner class is if the inner class is something that needs to *extend* a class that is different from the outer class.
But regardless, you always must ask, "Does it make sense to have this be an inner class?" "Am I limiting reuse of the class by making it an inner class?"
Most of the time, you should not use an inner class unless you really are making a class which belongs as part of -- and ONLY as part of -- an enclosing class. A class that could have no meaning or life outside the enclosing class. In the Zoo/ZooTycoon example, the relationship seems more like a "ZooTycoon USES a Zoo" rather than "Zoo" is a class whose sole purpose is to support and help the ZooTycoon. Someon else might want to use Zoo, without having to go through ZooTycoon! A customer, a vet, a school... whatever
Eithe way, it sounds fun
cheers,
Kathy
Al Olivera
Greenhorn

Joined: May 27, 2003
Posts: 3
Hi Kathy,
Thanks for the detailed reply (which I'm still digesting). Somehow, with help from you and Michael and some other folks on this thread, I made it work, but I'm still trying to understand how it all fits together.
I just bought your book btw, and think it's great. It's only the 5th book I've purchased on java, and I think it's the best!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: non-static var from static context