• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

When static variables get initialized?

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm, seems like thread is created first before main when I run it.



thread is running!
10
main is running
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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


Bu.
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic