Emiko Kezuka wrote:
> 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.
I think I see where your confusion lies. Let's take the following class:
public class Sample {
private int memberInstance = 0;
private static int staticInstance = 0;
public Sample() {
staticInstance++
}
public void normalTester() {
memberInstance = staticInstance;
}
public static int staticTester() {
// System.out.println ("memberInstance = " + memberInstance); <-- not
allowed
1 int methodInstance = staticInstance;
2 System.out.println ("staticInstance0 = " + ++staticInstance);
3 System.out.println ("staticInstance1 = " + ++staticInstance);
4 System.out.println ("staticInstance2 = " + ++staticInstance);
5 System.out.println ("staticInstance3 = " + ++staticInstance);
6 System.out.println ("Original value was " + methodInstance);
}
}
Some general statements:
* There is one and only one occurrence of the staticInstance member
field and the staticTester() method. These exist before any Sample
objects are created and may be accessed even if no Sample objects have
been created.
* Each Sample object that is created gets its own "copy" of non static
member fields and methods.
* The assignment in normalTester() is valid because all member fields,
static and non-static, are accessable from non-static methods.
* The same access is not allowed within static methods. It may access
only static fields and methods.
* A static method may declare variables local to itself. All methods may
do so, static or not. The space for these variables is allocated
dynamically on the call stack, so these variables exist only when the
method is invoked and cease to exist when the method exits. As the call
stack is built every time a method is called, the space for these
variables could actually be looked at as belonging to the caller of the
method. Even if several dozen threads in an application all called
Sample.staticTester(), there would be several dozen instances of the
local variable methodInstance. So as the method code executes, it
accesses the methodInstance variable that is on the call stack of the
currently executing thread.
* This is what makes a method "reentrant." Being static or non-static
has nothing to do with being reentrant.
* So you do not have to be thread aware within a method as you
manipulate variables local to the method. The method must be thread
aware if it manipulates the value of a class field.
Let's say a thread called Sample.staticTester() when the staticInstance
field contained the value 10. The first thing it does is store the value
of the static field into a local variable (line 1). So methodInstance
would also contain the value 10. Then it executes the next two lines and
the output will be:
staticInstance0 = 11
staticInstance1 = 12
Now suppose between lines 3 and 4, the thread is interrupted and another
thread starts executing. This thread also calls Sample.staticTester().
At this point, the static field staticInstance contains the value 12. So
another local copy is made. Let's say the method executes uninterrupted.
We would see:
staticInstance0 = 13
staticInstance1 = 14
staticInstance2 = 15
staticInstance3 = 16
Original value was 12
Now the second thread ceases to execute and the first thread continues
at line 4:
staticInstance2 = 17
staticInstance3 = 18
Original value was 10
As you can see, the method local variable was immune to change due to
being interrupted. So if the method only manipulates local variables
(including any parameters being passed to it), you don't have to worry
about synchronizing the code. If, like the example above, the method
manipulates static variables and you need predictable values at all
times, then you must synchronize the method (or critical segments within
the method).
Again, this is true of all methods, whether static or not static. The
only difference is that the non-static methods would have access to all
the class fields, so they have to be more mindful if the application is
set up so a single object's methods might be called by more than one
thread .
> I would be delighted if someone can makes me wise.
As would we all. 8^)
Tomm
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm