sabu
sabu

Reputation: 2077

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?

Upvotes: 4

Views: 6145

Answers (7)

Pshemo
Pshemo

Reputation: 124275

In your code

Temp t = new Temp(){
    {
        System.out.println(" instance initialize");
    }
};

you are creating object of anonymous class which extends Temp class.

Creating object of Subclass:

initialize block from Superclass
constructor of Superclass
initialize block from Subclass
constructor of Subclass

Upvotes: 2

NPE
NPE

Reputation: 500853

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).

Upvotes: 6

Ryan Stewart
Ryan Stewart

Reputation: 128909

Point 1: To be clear, you have two instance initializers: one in the Temp class and one in the anonymous inner class created in the main() method that is a subclass of Temp.

Point 2: Instance initializers aren't actually run before constructors. Per the JLS, they're run during a constructor, after delegating to the super constructor and before initializing instance fields and completing the constructor.

Point 3: In your code, each initializer is correctly executed at its appropriate time. I think you're expecting the second one to execute at the same time as the first one, but that would be incorrect because as pointed out in Point 1, they're initializers for two different classes.

Point 4: You may also be confused between static initializers and instance initializers. They're two distinct things.

Upvotes: 1

npe
npe

Reputation: 15719

What you are actually creating is not a Temp class instance, but an instance of some anonimous class, that inherits from Temp.

Thus, at first, the Temp initializers are called (anonimous block inside Temp and its construstor) and initializers in anonimous class are called afterwards.

Upvotes: 2

rsp
rsp

Reputation: 23373

The inline initialisation block is called after the constructor of the base class of the anonymous class you are instancing at the moment and before the empty implicit constructor of the anonymous class itself.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503090

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.

Upvotes: 16

firexfighterx
firexfighterx

Reputation: 679

The object must be in memory first before anything else can be done to it. The object is constructed in memory and then your console prints happen.

Upvotes: 0

Related Questions