views:

395

answers:

4

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?

+2  A: 

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.

Rahul Garg
A: 

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.

ykaganovich
What you have their is technically an "instance variable initialiser" not an "instance initialiser" (a block directly nested within a class). See JLS 3rd section 8.6.
Tom Hawtin - tackline
Oh, all right. Changed the example.
ykaganovich
+7  A: 

This seems to explain it well:

"Instance initializers are a useful alternative to instance variable initializers whenever: (1) initializer code must catch exceptions, or (2) 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 here.

javamonkey79
On the other hand you can write code once in one constructor and just call it from all other constructors. But anonymous inner classes make a good point.
Tadeusz Kopec
You can call it from other constructors - but then you are again repeating the call. If you add a new constructor, you have to remember to add the call to it. Not so with an instance initializer.
talonx
+1  A: 

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.

CPerkins