Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline initialization blocks in java

I have a class

public class MyMain{
    public static void main(String... arg){
            Temp t = new Temp(){
                {
                    System.out.println(" instance initialize");
                }
            };

        }
    }

class Temp{
    int i;

    {
        i=9;
        System.out.println("Static"+i);
    }
    Temp(){
        System.out.println("Temp const "+i);
    }
}

When i execute the main method the output comes:

Static9
Temp const 9
instance initialize

Ideally, the blocks are executed before the constructor, but the inline initialization block is called after the Constructor. Why?

like image 791
sabu Avatar asked Nov 27 '22 11:11

sabu


2 Answers

You're creating a subclass of Temp. For each class, any instance initializers are executed before the constructor body - but the superclass goes through initialization before the subclass initialization. So the execution flow is:

  • Initializers in Object
  • Constructor body in Object
  • Initializers in Temp
  • Constructor body in Temp
  • Initializers in anonymous class
  • Constructor body in anonymous class (none)

I would strongly advise you to refactor any code which looked like this anyway - aim for clarity rather than cleverness.

like image 142
Jon Skeet Avatar answered Dec 05 '22 09:12

Jon Skeet


JLS 12.5 spells out the order in which things happen during construction (emphasis mine):

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

(3) This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

(4) Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.

(5) Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

To summarize, superclass constructors (step 3) are executed before instance initializers (step 4). Both are executed before "the rest of the body of this constructor" (which you don't have in your example).

like image 28
NPE Avatar answered Dec 05 '22 09:12

NPE