Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Difference between static and non static variables in multi threading

807580Apr 23 2010 — edited Apr 23 2010
I dont understand the significant difference between using static member variable and non static variable in multi threading.
Are they inter-changable?
Example below..


public class StaticThread {
static int staticCounter=10;
int counter=10;


class Thread1 extends Thread
{
public void run()
{
staticCounter++;
counter++;
System.out.println("staticCounter: "+staticCounter);
System.out.println("counter :"+counter);
}

}



public static void main(String args[]) throws InterruptedException
{
StaticThread c=new StaticThread();
for (int i=0;i<5;i++)
{
Thread worker=c.new Thread1();
worker.start();


Thread.sleep(1000);
}
}


}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 21 2010
Added on Apr 23 2010
5 comments
664 views