87

Possible Duplicate:
Static Initialization Blocks

Consider the following code:

public class Test {
    {
        System.out.println("Empty block");
    }
    static {
        System.out.println("Static block");
    }
    public static void main(String[] args) {
        Test t = new Test();
    }
}

We understand that first the static block would be executed followed by the empty block. But the problem is that I have never been able to understand the real utility of an empty block. Can anyone show a real example in which -

  • Both static and empty blocks are being used
  • Both static and empty blocks have different utilities
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Anshu
  • 7,783
  • 5
  • 31
  • 41
  • 6
    An "empty" block would .. contain no content. Using more appropriate terminology helps. –  Sep 23 '12 at 06:29
  • @Anshu Have you checked this one : http://stackoverflow.com/questions/2420389/static-initialization-blocks ? – RAS Sep 24 '12 at 06:21
  • Yes, I checked this one, its the same link mentioned in below answer. – Anshu Sep 24 '12 at 07:31
  • See over there, super explanation : http://blog.sanaulla.info/2008/06/30/initialization-blocks-in-java/ – Benj Mar 05 '13 at 11:00

2 Answers2

116

They're for two very different purposes:

  • The static initializer block will be called on loading of the class, and will have no access to instance variables or methods. As per @Prahalad Deshpande's comment, it is often used to create static variables.
  • The non-static initializer block on the other hand is created on object construction only, will have access to instance variables and methods, and (as per the important correction suggested by @EJP) will be called at the beginning of the constructor, after the super constructor has been called (either explicitly or implicitly) and before any other subsequent constructor code is called. I've seen it used when a class has multiple constructors and needs the same initialization code called for all constructors. Just as with constructors, you should avoid calling non-final methods in this block.

Note that this question has been answered many times in stackoverflow and you would do well to search and review the similar questions and their answers. For example: static-initialization-blocks

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
26

The static block is executed whenever your class loads. The empty block is executed whenever you instantiate your class. Try comparing these:

1.

public static void main(String[] args) {
    Test t = new Test();
}

2.

public static void main(String[] args) {

}

Outputs:

1.

Static block
Empty block

2.

Static block

In Layman words, static block only gets called once, no matter how many objects of that type you create.

Jaimin Patel
  • 4,559
  • 3
  • 32
  • 35
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • The OP `never been able to understand the *real utility* of an empty block` or a static block for that matter. – asgs Sep 23 '12 at 06:16
  • 2
    @asgs The 'real utility' is that they behave differently and as this answer describes. – user207421 Sep 23 '12 at 06:34
  • @EJP right, i guess what the OP all wants to see is a REAL example, e.g. something somewhere used in a public library or even the JDK. – asgs Sep 23 '12 at 06:43
  • 1
    @arshaji I just tried your example, and I found that the second example doesn't execute the static block, unless I instantiate it. – Quazi Irfan Jan 24 '15 at 22:51
  • @arshajii can you clarify what it means by loading a class? I was trying to understand on when the static final variables are initialised without the help of constructor. I have mentioned my doubt here: https://ideone.com/FPIbuR would be great if you could help me on it. – unknownerror Jul 03 '19 at 16:52