Overloaded Constructors: Instance Initializer

Asked

Viewed 536 times

2

From the perspective of compiler behavior and java class design what would be the justification for using the Instance Initializer block?

Considering the following class:

class Caneta {
    public Caneta() {
        System.out.println("Caneta:constructor");
    }
    public Caneta(String a) {
        System.out.println("Caneta:constructor2");
    } 

    /** ################################### */
    /** Inicializador de Instância */
    /** ################################### */
    {
        System.out.println("Caneta:init1");
    }
    
    /** ################################### */
    /** Inicializador de Instância */
    /** ################################### */
    {
        System.out.println("Caneta:init2");
    }
    /** ################################### */

    public static void main(String[] args) {
        new Caneta();
        new Caneta("aValue");
    }
}

The output of the previous code is:

Caneta:init1
Caneta:init2
Caneta:constructor
Caneta:init1
Caneta:init2
Caneta:constructor2

Why do you think you need an instance initializer if you can initialize your instances using constructs?

Reference:

[MALA GUPTA, 2015], OCP Java SE 7 Programmer II
Certification Guide
: PREPARE FOR THE 1ZO-804 EXAM

  • @Tiagos, the two questions I asked are different. A question about the behavior of overloaded methods and another question about using Instance Initializer from the compiler and java class design point of view. Are not duplicates!

1 answer

2

This is a good explanation:

Instance initializers are useful alternatives to instance variable initializer when:

  • The startup code should capture exceptions;
  • Perform extensive calculations that cannot be represented with an instance variable initializer.

You can, of course, always write such code in constructors. However, in a class that has multiple constructors, you would repeat the code in each constructor. With this alternative, you can write the code only once, and it will run regardless of which constructor is used to build the object. They are also useful in Inner anonymous class, which cannot declare a constructor.

These initializers (as well as variable initializers) cannot refer to variables declared textually later in the code. When an object is created, the initializers are executed in textual order - their appearance order in the source code. This rule helps prevent initializers from using instance variables that have not yet been initialized correctly.

Source: Javaworld Object initialization in Java.

  • 1

    To be registered: it is not necessary to repeat the code in each constructor, it is possible for one constructor to call the other with this();

  • @Murillogoulart, vc did not consider that initiators cannot reference variables. Javaworld Object initialization in Java: When an object is created, the initializers are executed in textual order - their appearance order in the source code. This rule helps prevent initializers from using instance variables that have not yet been initialized correctly. If possible, you can improve your answer?

  • @pss1support This rule is not unique to this type of initializer. It applies to variable initializers as well. Still, I added.

  • 1

    @Murillogoulart, that’s right. True, but the goal is the more complete the better the answer. I would like to know this when using the Instance Initializer blocks. This is relevant information. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.