This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in static block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in static block" Watch "Doubt in static block" New topic
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in static block
 
Similar Threads
run time error
static keyword
method modifiers
Question on OverLoading
Is it possible to invoke a method before even the object gets created?