4

I found a 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 & good since I'm 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

Algorithmist
  • 6,657
  • 7
  • 35
  • 49
Brinal
  • 172
  • 2
  • 18
  • Regarding the additional questions (compilation, loading, execution, rt.jar): they are unrelated and don't belong here. I suggest you read some basic material and *then* ask more concrete questions. – Joachim Sauer May 31 '11 at 13:11

5 Answers5

7

When you run any Java program the Main thread is the first thread to start up.

The output you are seeing isn't indicating that the main method is executing. Rather it is the main thread.

So, anytime you fire up a Java program, you will have a thread called main executing. And, if that thread immediately exits the JVM, then that's all of the threads that will ever run.

To clarify:

As per my understanding static block is executed during the load time.

The static block is executed when the class is loaded. This happens by a class loader, and is executed in the main thread when a Java program starts up.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • You mean to say static block is loaded when the class is loaded and executed when main thread starts up. Just to claarify the – Brinal May 31 '11 at 14:39
  • 1
    @Brinal, the order is as follows: Main thread starts, class gets loaded, static block executes. (After the classes were all loaded, the main method would execute.) – jjnguy May 31 '11 at 14:43
3

The main class is loaded and initialized on the main thread. Although this is not documented explicitly anywhere (as far as I know), it's a pretty safe assumption, as there's hardly a reason to implement it differently.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • @Joachim Sauer: for my knoledge: main class is loaded in main thread but who initiate mail thread? is it JVM? – Harry Joy May 31 '11 at 13:11
  • Yes, the JVM starts that thread, exactly the same way it does when you don't have a `static` initializer block. The only difference here is that some user code runs on it before you *usually* see it. *Usually* the first sign of the `main` thread is when the `main` *method* gets executed. – Joachim Sauer May 31 '11 at 13:12
  • @Joachim Sauer: it means JVM will start thread for a class whether there is a main method exist or not. – Harry Joy May 31 '11 at 13:13
  • @Harry: the `main` method *doesn't* start the `main` thread. It simply **runs** in it. – Joachim Sauer May 31 '11 at 13:14
1

All Java code executes on some thread. Usually (and unsurprisingly) the main thread is named, "main". Normally, the main thread loads the main class (which executes the static blocks) and then calls your main method.

However, you don't appear to have a main method, so if it had made it out of the static block, your program would have crashed with an exception about main not being found.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

Something must have loaded your SimpleTest class, which must have been the main thread (as the current thread's name indicates). Without seeing the invoking code it's hard to know what's happening here - but something is causing the SimpleTest class to be loaded and it's running off the main thread.

planetjones
  • 12,469
  • 5
  • 50
  • 51
0

Static initialization is not always are executed on the main thread

If that class gets referenced from another class within another thread, the static initialization will not occur on the main thread but rather in the background thread where it gets invoked from for the first time.

Gubatron
  • 6,222
  • 5
  • 35
  • 37