• Post Reply Bookmark Topic Watch Topic
  • New Topic

static block

 
Urs Waefler
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

This is my code:

class B {

static {
B x = new B ();
}


public static void main (String [] args) {


}
}

I do not properly understand static block. Could anybody give me some hints. What is it?

Regards
Urs
 
Campbell Ritchie
Marshal
Posts: 53217
124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand your question correctly, what you have is called a static initialiser. There is discussion on the Ranch in several places, this, and this, which is maybe less helpful, being two recent examples I found with a search.
 
suji kumar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static block will be executed when the class gets loaded...
 
Murad Iqbal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, the code in a static initializer block is executed by the virtual machine when the class is loaded.
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!