Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static block

 
Urs Waefler
Ranch Hand
Posts: 77
  • 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
Sheriff
Posts: 48652
56
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic