The Artima Developer Community
Sponsored Link

Java Answers Forum
Calling non-static variable externally

3 replies on 1 page. Most recent reply: Jan 25, 2007 6:20 AM by Andrew H

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Andrew H

Posts: 2
Nickname: vesper
Registered: Jan, 2007

Calling non-static variable externally Posted: Jan 24, 2007 7:54 PM
Reply to this message Reply
Advertisement
I need to call a non-static variable from an external object class. When I call the variable using class_name.var_name, it says that is being called from a static context. The method it is being called from isn't static, so I'm not sure why its saying that.

I can't just make the variable static because it needs to be implemented with the paint method.

Sorry if this is a stupid question...but thanks for any help.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Calling non-static variable externally Posted: Jan 24, 2007 10:15 PM
Reply to this message Reply
class ExampleClass {
    public int nonStaticValue;
    public static int staticValue;
}
 
class CallerClass {
    public void someMethod() {
        int i = ExampleClass.staticValue; //works
        i = ExampleClass.nonStaticValue; //does not work
        ExampleClass exampleInstance = new ExampleClass();
        i = exampleInstance.nonStaticValue; //works
        i = new ExampleClass().nonStaticValue; //works
    }
}
 
A non-static variable [b]does not exist[/b] until you create an instance of the class. And also then you must call the variable using the instance, not the class name.
Only static variables and methods are visible using the class name.
 
 
quote:"I can't just make the variable static because it needs to be implemented with the paint method.
". Say what? "static" does not mean "final". I don't really understand why the variable can't be static.
The only thing you must pay attention to is that a static variable is independent from the instance. Means: If you create multiple instances of a classs, they will all have the same value for a static variable. If you change it for one instance, it's also changed for the other instances.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Calling non-static variable externally Posted: Jan 24, 2007 10:18 PM
Reply to this message Reply
I forgot to use the java tag after the source code, that's why everything is highlighted. Just ignore it.

Andrew H

Posts: 2
Nickname: vesper
Registered: Jan, 2007

Re: Calling non-static variable externally Posted: Jan 25, 2007 6:20 AM
Reply to this message Reply
thanks

Flat View: This topic has 3 replies on 1 page
Topic: Calling non-static variable externally Previous Topic   Next Topic Topic: How compile jmf programm?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use