• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in static block

 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...");
}
}
 
muskaan gaffor
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"abhijit GHY"

Please check your private messages.
[ February 05, 2008: Message edited by: Ben Souther ]
 
T Duffy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic