Hi there,
It goes like this, static methods are loaded when the JVM starts and they
serve all instances of the same class. Meaning you can use these methods via
the class's instances or via the class's name:
public MyClass {
public static void staticMethod(){
System.out.println("in static method");
}
MyClass.staticMethod(); //via class itself
MyClass mc = new MyClass();
mc.staticMethod(); //via an instance
Now, to the variables issue, any method may use local variables (declared
within the method), since they are declared inside the method, there is no
problem working with such variables. Static methods may also use member
variables only and ONLY if they are declared static as well, this is since
all static data is loaded when the JVM starts, thus, static variables are
loaded as well, and they are accessible to static methods. Instance
variables are loaded only when there is a new instance of the class, hence
there is no access to these variables from static methods, they still don't
exist when a static method tries to call them (via class name).
If you wonder about reliability of the variables when dealing with multi
threaded application, which accesses static methods (with local variables),
well, maybe that's the time to think about synchronizing the method. :o)
That's it I think...
Hope that helped!
David Treves.
----- Original Message -----
From: "Emiko Kezuka" <[EMAIL PROTECTED]>
To: "JDJList" <[EMAIL PROTECTED]>
Sent: Monday, June 10, 2002 8:35 AM
Subject: [jdjlist] variables in a static method
> Hi, everyone.
>
> I'm a bit confusing.
> Could someone tell me whether my understanding is right or not
> and if it is not, in what part...
>
> Static method (in a class) is a method which NEVER be instanciate.
> that is, Static method is used as it is and never allocated anew for each
uses.
> that is, you cannot use instance variables (I mean by "instance variables"
the
> variables which is declared within the method) , or the value of that
variables
> can be altered in any time (by another call of this method from
elsewhere
> which tries to run this very method from the beginning and may alter the
value
> along to it).
>
> Of course, if the variable declared within a method have initial value
> and never changed its value throughout the method, it seems OK...
> for you can get the same value all the time being...
> But if you change the value, it will affect to others and the result
> might be untrustworthy.
>
> Uh...
> Are there any flaw in this thinking? For I found some code openly
available
> contains static methods which declare variables in them.
>
>
> I would be delighted if someone can makes me wise.
> Thank you.
>
> Emiko Kezuka
>
>
>
> To change your membership options, refer to:
> http://www.sys-con.com/java/list.cfm
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm