Static block vs. initializer block in Java?

70,599

Solution 1

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

Solution 2

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.

Share:
70,599
Anshu
Author by

Anshu

Thanks for visiting my profile. A java expert with 15+ years of experience LinkedIn profile: http://www.linkedin.com/profile/view?id=19629371

Updated on July 08, 2022

Comments

  • Anshu
    Anshu almost 2 years

    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
  • asgs
    asgs over 11 years
    The OP never been able to understand the *real utility* of an empty block or a static block for that matter.
  • Prahalad Deshpande
    Prahalad Deshpande over 11 years
    In addition, a point to note is that static blocks are very useful to instantiate static class level variables (variables which will be shared across multiple class instances).
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels over 11 years
    @Prahalad: good point. Thanks.
  • user207421
    user207421 over 11 years
    The anonymous initializer(s) is (are) called during the constructor, not before it. Specifically, immediately after the super() call.
  • user207421
    user207421 over 11 years
    @asgs The 'real utility' is that they behave differently and as this answer describes.
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels over 11 years
    @EJP: that's very important information and will require a revision in my answer. Many thanks for the correction!!!
  • asgs
    asgs over 11 years
    @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.
  • Frank Pavageau
    Frank Pavageau over 11 years
    The non-static block is also useful to initialize fields of an anonymous class, since it cannot have a constructor (being anonymous).
  • Aseem Bansal
    Aseem Bansal over 10 years
    @EJP Is there a source for that information about the sequence of these calls(super->anonymous blocks->constructor code)? It would be nice to have the source also
  • Quazi Irfan
    Quazi Irfan over 9 years
    @arshaji I just tried your example, and I found that the second example doesn't execute the static block, unless I instantiate it.
  • unknownerror
    unknownerror almost 5 years
    @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: ideone.com/FPIbuR would be great if you could help me on it.