aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes When static variables get initialized? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "When static variables get initialized?" Watch "When static variables get initialized?" New topic
Author

When static variables get initialized?

Ravindranath Chowdary
Ranch Hand

Joined: Nov 08, 2006
Posts: 71
Hi friends,

I have question on when static variables gets initialized.
I read in a book that all the static variables get initialized as soon as the class gets loaded and before the main method starts execution.

In the below program t1 is a static reference variable in the constructor I am creating a thread.

But I noticed that first main thread is getting created later the following variable is getting called...
static Test1 t1 = new Test1(); and new thread is getting created.


public class Test1 implements Runnable{
Thread t;
static Test1 t1 = new Test1();
public static void main(String[] args) {
int i = 10;
System.out.println(i);

}
Test1(){
t = new Thread(this);
t.run();
}
public void run(){
try{
Thread.sleep(10000000);
}catch(InterruptedException e){

}
}
}

As the static variables get initialized after class gets loaded. Then why the thread in the Test1 constructor is created after the main thread.


Thanks,
Ravindranath.
Tony Smith
Ranch Hand

Joined: Jul 07, 2007
Posts: 229
Hmmm, seems like thread is created first before main when I run it.



thread is running!
10
main is running
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy cowboys!

The question in the topic line was "When static variables get initialized?". And the correct answer was already given by Ravindranath.
Q:
"As the static variables get initialized after class gets loaded. Then why the thread in the Test1 constructor is created after the main thread."

Why do you think so?
First [i]initialization[i] of static variables is indeed after the class has been loaded. In your constructor you only refer a new thread object to the variable, that is not initialization. In this context this is unimportant. Then you made a mistake, you run() the thread, however you should start() it. When you run it, it doesn't execute as an own thread, just within the current thread (main!) it runs the run() method and sleeps for ~2 3/4 hours.
When you wait long enough, you will see your int (from the main method) printed.

But all this has nothing to do with initialization.

Your code starts, sleeps 2,777 hours and then prints 10.
What did you expect?


Bu.


all events occur in real time
Ravindranath Chowdary
Ranch Hand

Joined: Nov 08, 2006
Posts: 71
Hi Bu,
The code should be like this, I did a mistake...

public class Test1 implements Runnable{
Thread t;
static Test1 t1 = new Test1();
public static void main(String[] args) {
int i = 10;
System.out.println(i);

}
Test1(){
t = new Thread(this);
t.start();
}
public void run(){
try{
Thread.sleep(1000);
}catch(InterruptedException e){

}
}
}


I am using the Ecipse IDE, in that I noticed as...
At the first line of the main method...in the debug console of Ecipse it is shown as
Thread[main] suspended...and now the control is at
first line of the constructor and in the second line of the constructor a new thread is getting created.
My question is why the main thread is getting created before the thread in the constructor starts.



Thanks,
Ravindranath.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi,

the main thread is always the first thread started. It will be started by the VM when you type
java Test1


Bu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When static variables get initialized?