Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Static Initializer Block

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We all know that "Static initializer block" executes at the Class loading time in java, and we can also call main method or normal methodp from inside the "Static initializer block"...... but here below given a sample code in which, main method is executing twice;

1st time i am calling it from inside the "Static initializer block"
But 2nd time main method get executed automatically by jvm before ending-up the execution of "Static initializer block" ( unable to get the flow of this program ??? )

As per my knowledge highlighted part of the program should execute first before 2nd time execution of main method but thts not happening here

public class StaticBlock {

static int i=10;

static {
System.out.println("my static block 1");

// You can use labeled blocks inside static blocks
printingThread:{
// static block is executed in main thread
System.out.println("Current thread "+Thread.currentThread());
}
}

static {
System.out.println("In static block "+i);
//The variables you declare in a static block aren't a part of the object. They are not
//even the static variables of the class, they just belong to that block only.
String[] arr = null;

// you can call main() too
main(arr);

// You can also execute threads
new Thread(new Runnable(){
public void run() {
for(int i=1;i<=5;i++)
System.out.println(Thread.currentThread().getName()+" "+i);
}
},"First thread").start();

// You can't do this, myMeth() is non-static
new StaticBlock().myMeth();
}


public void myMeth() {
System.out.println("In a non static method");
}


public static void main(String[] args) {

System.out.println("In main()");
}
}
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The highlighted code will be executed before main method is executed for the second time.
By that, I mean that Thread.start() will be called before main method is executed for the second time.

After that point the output is nondeterministic as there are two running threads writing to the console at the same time.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kr. Gaurav wrote:We all know that "Static initializer block" executes at the Class loading time in java...


It might be worth mentioning that, while all this may be fun to investigate (and don't let me stop you) static initialisers are pretty rare - mainly because there's usually a better alternative.
I think I've maybe written a dozen of 'em in 14 years.

So...don't spend too much time on this.

Winston
 
What's that smell? Hey, sniff this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic