• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about local variables in a static method

 
Yeray Santana Borges
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Yeray Santana Borges
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic