This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Doubt in static block

 
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...");
}
}
 
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 ]
 
Ranch Hand
Posts: 147
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • 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 ]
 
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic