Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between an instance initializer and a constructor?

Tags:

Just wondering about the reason of compiling code like this:

class MyClass extends AnotherClass {
  {
    MySecondClass object = new MySecondClass();
    object.doSomething();
  }
}

Whats the difference between this code and code in constructor? This code executes before the object creation.

like image 517
Oldestkon Avatar asked Jun 08 '13 15:06

Oldestkon


People also ask

What is difference between instance initializer block and constructor?

Initializer block : contains the code that is always executed whenever an instance is created. It is used to declare/initialize the common part of various constructors of a class. Constructors : are used to initialize the object's state.

What is the difference between instance and constructor?

Q1. What is the difference between constructor and instance initialization blocks ? Ans. Constructor has the same name as class name whereas instance initialization block just have a body without any name or visibility type.

What is an instance initializer?

Instance initializer block works are used to initialize the properties of an object. It is invoked before the constructor is invoked. It is invoked every time an object is created.

Is a constructor and initializer?

Constructor is a special non-static member function of a class that is used to initialize objects of its class type. In the definition of a constructor of a class, member initializer list specifies the initializers for direct and virtual bases and non-static data members.


2 Answers

The code inside the braces with no names will be part of the constructor of the class and be executed before the logic contained in the class constructor.

Quick example:

public class Foo {
    {
        System.out.println("Before Foo()");
    }

    public Foo() {
        System.out.println("Inside Foo()");
    }

    {
        System.out.println("Not After Foo()");
    }
}
like image 186
Luiggi Mendoza Avatar answered Sep 20 '22 22:09

Luiggi Mendoza


This is called an instance initializer. The code in the initializer is inserted after the call to the super class constructor and before the rest of the constructor code.

The first operation of any constructor is to invoke a super class constructor. If a constructor is called explicitly, super(...), the specified constructor is used. If no constructor is explicitly invoked, the default constructor (with no arguments) is invoked in the super class. If no such constructor exists, it is a compile time error.

After this explicit or implicit constructor invocation, instance initializers are invoked in the order they appear in source code (yes, you can have more than one initializer).

To illustrate, running this program prints

Another constructor
Init 1
Init 2
Test constructor
class Another {
  Another() { System.out.println("Another constructor"); }
}

class Test extends Another {

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

  { System.out.println("Init 1"); }

  Test() { System.out.println("Test constructor"); }

  { System.out.println("Init 2"); }

}

The most commonly seen application is in the initalization the "double brace initialization" idiom, where an anonymous inner class is defined, and an instance is created and configured at once. Here's a fairly common example from Swing programming:

JButton popupButton = new JButton(new AbstractAction("Popup") {
  {
    putValue(Action.SHORT_DESCRIPTION, "Popup a dialog");
  }

  @Override
  public void actionPerformed(ActionEvent evt)
  {
    popup();
  }
});

This could be useful if you have multiple constructors, and need to perform some parameter-less initialization in every constructor. This could be factored into an initialization block.

like image 31
erickson Avatar answered Sep 17 '22 22:09

erickson