How is an instance initializer different from a constructor?

JavaInstantiation

Java Problem Overview


In other words, why would you need an instance initializer? What difference or advantage do you have in writing a instance initializer over a constructor?

Java Solutions


Solution 1 - Java

This seems to explain it well:

> Instance initializers are a useful alternative to instance variable > initializers whenever: > > + initializer code must catch exceptions, or > > + perform fancy calculations that can't be expressed with an instance variable initializer. You could, of course, always write such code in > constructors. > > But in a class that had multiple constructors, you would have to repeat the code in each constructor. With an instance initializer, you > can just write the code once, and it will be executed no matter what > constructor is used to create the object. Instance initializers are > also useful in anonymous inner classes, which can't declare any > constructors at all.

From: JavaWorld Object initialization in Java.

Solution 2 - Java

In terms of object lifecycle, there is no difference. Both are invoked at construction time, and logically the initializer block can be considered part of construction.

Semantically, an initializer is a nice tool to have for several reasons:

the initializer can improve code readability by keeping the initialization logic next to the variable being initialized:

   public class Universe {
       public int theAnswer;
       {
         int SIX = 6;
         int NINE = 7;
         theAnswer = SIX * NINE;
       }

       // a bunch of other vars
   }

vs

   public class Universe {
       public int theAnswer;

       // a bunch of other vars

       public Universe() {
         int SIX = 6;
         int NINE = 7;
         theAnswer = SIX * NINE;

         // other constructor logic
       }
   }

Initializers are invoked regardless of which constructor is used.

Initializers can be used in anonymous inner classes, where constructors can't.

Solution 3 - Java

When you have many constructors and want some common code to be executed for each constructor you can use instance initializer.As it is called for all constructors.

Solution 4 - Java

I would avoid the instance initializer idiom in general - the only real advantage it gives over variable initializers is exception handling.

And since an init method (callable from constructor) can also do exception handling and also centralizes constructor setup code, but has the advantage that it can operate on constructor parameter values, I'd say that the instance initializer is redundant, and therefore to be avoided.

Solution 5 - Java

The real advantage of instance initializers over constructors is seen when we use an anonymous inner class.

Anonymous inner classes can't have a constructor (as they're anonymous), so they're a pretty natural fit for instance initializers.

Solution 6 - Java

At the time of object creation, if we want to perform initialise of instance variables, then we should go for Constructor, other than initialisation activity if we want to perform any activity at the time of object creation then we should go for instance block.

We can't replace constructor with instance block because constructor can take argument but instance block can't take arguments.

We can't replace instance block wih constructor because a class can contain more than one constructor. If we want to replace instance block with constructor then in every constructor we have to write instance block code because at runtime which constructor will be called we can't expect, this will unnecesarily increase duplicate code.

Example :

class MyClass{

    static int object_count = 0;

    MyClass(){
	    object_count++;
    }

    MyClass(int i){
	
	    object_count++;
    }

    void getCount() {
	
	    System.out.println(object_count);
    }

    public static void main(String... args) {
	    MyClass one = new MyClass();
	    MyClass two = new MyClass(2);
        two.getCount();
    }
}

Output : 2

class MyClass{

    static int object_count = 0;

    {
        object_count++;
    }
    
    MyClass(){
	    
    }

    MyClass(int i){		
	    
    }

    void getCount() {
	
	    System.out.println(object_count);
    }

    public static void main(String... args) {
	    MyClass one = new MyClass();
	    MyClass two = new MyClass(2);
        two.getCount();
    }
}

Output : 2

Solution 7 - Java

Initializer is way to share code between constructors and it make code more readable if initializer is used with variable declaration.

The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors. Oracle documentation

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAjayView Question on Stackoverflow
Solution 1 - Javajavamonkey79View Answer on Stackoverflow
Solution 2 - JavaykaganovichView Answer on Stackoverflow
Solution 3 - JavaRahul GargView Answer on Stackoverflow
Solution 4 - JavaCPerkinsView Answer on Stackoverflow
Solution 5 - JavapadippistView Answer on Stackoverflow
Solution 6 - JavaNPEView Answer on Stackoverflow
Solution 7 - JavadnyaneshView Answer on Stackoverflow