| Author |
Doubt in static block
|
Abhijit Das
Ranch Hand
Joined: Sep 25, 2007
Posts: 156
|
|
Hello, A program must starts with the main thread( as a primary thread).The following program is running and shows the output. Now, here which thread is running to execute the program??? public class StaticBlock { static { func("JAVA"); System.exit(1); } public static void func(String s){ System.out.println(s + " is a wonderful language..."); } }
|
Abhijit Das
SCJP 5.0 | SCWCD 1.5
|
 |
muskaan gaffor
Ranch Hand
Joined: May 19, 2006
Posts: 50
|
|
Are you sure?? Cause , I have used the same program and tried execute it. Its showing no main method found. regards, Mussy
|
 |
Abhijit Das
Ranch Hand
Joined: Sep 25, 2007
Posts: 156
|
|
hi,Gaffor You can try to execute it in console (DOS promot)only , not in IDE. but, I am getting output in both. [ February 04, 2008: Message edited by: abhijit GHY ]
|
 |
Jart Bo
Ranch Hand
Joined: Oct 22, 2007
Posts: 144
|
|
I've learned that static blocks run when the class is initialized. A class is initialized immediately before the first occurrence of any of the following: a. instance of the class is created b. a static method declared in the class is invoked c. a static field declared in the class is assigned It looks like abhijit's example demostrates b.
|
SCJP, SCWCD
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
"abhijit GHY" Please check your private messages. [ February 05, 2008: Message edited by: Ben Souther ]
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
T Duffy
Greenhorn
Joined: Feb 05, 2008
Posts: 1
|
|
I played with the code a bit and the answer seems to be the thread which loads the class the first time(only) is the thread which executes the static block and method. So in your example above the answer is the main thread. I used the code below if you want to play with it a bit: public class SCJA_4 { static { System.out.println("BLOCK: " + Thread.currentThread().getName()); func("JAVA"); Thread t = new Thread(new SCJA_4b()); t.setName("New thread."); t.start(); //new SCJA_4c(); System.exit(1); } public static void func(String s){ System.out.println(s + " is a wonderful language..."); System.out.println("FUNC: " + Thread.currentThread().getName()); } } class SCJA_4b extends Thread { static { System.out.println("BLOCK: " + Thread.currentThread().getName()); func("C++"); } public static void func(String s){ System.out.println(s + " is a wonderful language..."); System.out.println("FUNC: " + Thread.currentThread().getName()); } public void run () { new SCJA_4c(); } } class SCJA_4c { static { System.out.println("BLOCK: " + Thread.currentThread().getName()); func("PERL"); } public static void func(String s){ System.out.println(s + " is a wonderful language..."); System.out.println("FUNC: " + Thread.currentThread().getName()); } } The output of the above code is: BLOCK: main JAVA is a wonderful language... FUNC: main BLOCK: main C++ is a wonderful language... FUNC: main BLOCK: New thread. PERL is a wonderful language... FUNC: New thread.
|
 |
 |
|
|
subject: Doubt in static block
|
|
|