aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt about local variables in a static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt about local variables in a static method" Watch "Doubt about local variables in a static method" New topic
Author

Doubt about local variables in a static method

Yeray Santana Borges
Ranch Hand

Joined: Mar 02, 2008
Posts: 46
Hi,

Instance variables is on the heap, local variables is on the stack. Each thread have his own stack.

Local variables of static methods are static too?, where a thread put the variables of that static method? that variables are shared for all threads?

for example, method like this:

If i have some threads that invoke localStaticVariables()
there are one copy of variable j that is shared for all threads or each thread have his own copy of j variable in each own stack?

Thanks

[ July 20, 2008: Message edited by: Yeray Santana Borges ]
[ July 20, 2008: Message edited by: Yeray Santana Borges ]

SCJP 6 - SCWCD 5 - SCBCD 5
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
I am not sure about this ...
methods have local variables that are not declared on heap. Each thread will have its own set of variable. I tried to create a code to prove my point but it did not work out well.

EDITED....
Finally, i was able to create a code ...

the output is
Thread b1 prints 1-101 & then goes into wait.
Thread b2 prints 1-101 & then goes into wait. But notifies just before wait
Thread b1 resumes & prints 102-150. Notifies All before exiting Synchronized
Thread b2 resumes & prints 102-150.

Hope it helps.

Edited again ...
in run method if you declasre I as int than also it will give same result
[ July 20, 2008: Message edited by: Milan Sutaria ]
[ July 20, 2008: Message edited by: Milan Sutaria ]

SCJP 6 83%
Working on SCWCD/OCPJWCD
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
This Code may help you what you want to tell.

package serilazation;

public class StaticLocal extends Thread{

static void localStaticVariables(){
int j = 0;
j++;
System.out.println(Thread.currentThread().getName() + " : Valude : " + j);
}



public static void main(String[] args) throws Exception{
Thread b1 = new StaticLocal(){
@Override
public void run() {
while(true)
localStaticVariables();
}
};

Thread b2 = new StaticLocal(){

public void run() {
while(true)
localStaticVariables();
}
};

Thread b3 = new StaticLocal(){

public void run() {
while(true)
localStaticVariables();
}
};

b1.setName("b1");
b2.setName("b2");
b3.setName("b3");

b1.start();
b2.start();
b3.start();

Thread.currentThread().sleep(Long.MAX_VALUE);

}
}


Here each localStaticVariables method is static and having j as local variable. output is.

b2 : Valude : 1
b1 : Valude : 1
b3 : Valude : 1
b2 : Valude : 1
b1 : Valude : 1

which shows that each Thread is create new copy of local variable each time it not makes any difference for static method.. ya if you take this j as member variable then as per ilan Sutaria case will be different.. might this example will clear you drought very clary


kaushik Vira
-------------------------------------
SCJP, Preparing SCWCD..
Yeray Santana Borges
Ranch Hand

Joined: Mar 02, 2008
Posts: 46
Ok, it�s more clear for me now. After see this examples code i will assume that in all methods (static or not) local variables are created in the stack not in the heap, and for this reason, there are no problem about concurrent access with this local variables.

Thanks for your responses, it help me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt about local variables in a static method
 
Similar Threads
thread
Threads, wait method an spurious wakeup
Slap me silly, but...
Creating multiple children threads
Static methods and thread safety