• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static Block and Main Thread

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I found very interesting thing while trying with java . Please find the code below:

public class SimpleTest {
static{
System.out.println(Thread.currentThread().getName());
System.exit(0);
}
}

The above program runs without any exception(Well & goodsince im exiting in the static block itself) . But i got the following as the output:
main


Since i haven't started the main thread, how does it got created. As per my understanding static block is executed during the load time. Then how does main thread come into picture?

Can anyone please give the brief introduction how the compilation,loading and execution done in jvm ? Also the use of rt.jar?

Thanks in advance,
Brinal
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main method hasn't come into picture, but that doesn't mean the main thread hasn't. These are two different entities; the main thread calls the main method, but it also does a lot more.
 
Brinal Jason Machado
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Roob: Can you please describe in detail?
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the JVM starts, at least one thread is started - the main thread. (There are others but those are irrelevant in this story.) This main thread gets told to load the main class (as provided to the JVM on the command line, or through a JAR manifest entry). This class is then loaded and initialized, by the main thread. The class loading ensures the static block is executed. The main thread then usually tries to find the main method, using reflection. When it finds it, it invokes it. Otherwise a NoSuchMethodError is thrown. In both cases, after this the main threads ends.

In your case however, the static block calls System.exit, which exits the JVM. The main thread doesn't get the chance to find and invoke the main method.

Well, that's my interpretation of the process. The exact details are either locked away in the source code of the JVM (which fortunately is available for download now), or in the Java Language Specification. I haven't fully read the latter one, and haven't even looked at the latter one, so I can't tell you exactly where.
 
Brinal Jason Machado
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Rob: Thank you very much for the clarification
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic