Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static block vs. initializer block in Java? [duplicate]

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
like image 951
Anshu Avatar asked Sep 23 '12 06:09

Anshu


People also ask

What is the difference between static block and instance initializer block?

Static initializer block is executed when class is loaded into memory, while instance initializer block is executed when an instance of the object is created using the new() operator.

What is need of static and initializer block?

Java 8Object Oriented ProgrammingProgramming. Instance variables are initialized using initialization blocks. However, the static initialization blocks can only initialize the static instance variables. These blocks are only executed once when the class is loaded.

How many times is the static initializer block executed?

Therefore, only one time static block will be executed. Note: Instance block and constructor both are executed during the object creation but instance block will execute first before the execution of the constructor during the object creation.

Can we have multiple static block in Java?

Yes. It is possible to define multiple static blocks in a java class.


2 Answers

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

like image 104
Hovercraft Full Of Eels Avatar answered Oct 19 '22 22:10

Hovercraft Full Of Eels


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.

like image 40
arshajii Avatar answered Oct 19 '22 22:10

arshajii