It's not a secret anymore!
The moose likes Java in General and the fly likes static block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "static block" Watch "static block" New topic

static block

Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77

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?


SCJP 1.4
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46334
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

Joined: Nov 08, 2006
Posts: 10
static block will be executed when the class gets loaded...
Murad Iqbal
Ranch Hand

Joined: Dec 09, 2003
Posts: 90
Yup, the code in a static initializer block is executed by the virtual machine when the class is loaded.
I agree. Here's the link:
subject: static block
jQuery in Action, 3rd edition