2
votes

I know static contents are loaded first in the memory, but why is 'IT' printed before 'CT' when I haven't mentioned it as static?

class Person
{
    Person()
    {
        System.out.print(" CP");
    }

    static 
    {
        System.out.print("SP");
    }
}

class Teacher extends Person
{
    Teacher()
    {
        System.out.print(" CT");
    }

    {
        System.out.print(" IT");
    }
}


public class StaticTest 
{
    public static void main(String[] args) 
    {       
        Person p = new Teacher();
    }
}
1

1 Answers

6
votes

Initializer blocks such as {System.out.print(" IT");} are executed before the constructor. Actually, they are copied to the beginning of each constructor.

Initializing Instance Members

Normally, you would put code to initialize an instance variable in a constructor. There are two alternatives to using a constructor to initialize instance variables: initializer blocks and final methods.

Initializer blocks for instance variables look just like static initializer blocks, but without the static keyword:

{
// whatever code is needed for initialization goes here
}

The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.

(Source)

And to be more exact, here's the initialization order as described in the JLS :

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

  2. If this constructor begins with an explicit constructor invocation (ยง8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that 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 5.

  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.

Notice that the instance initializers are executed in step 4, prior to the body of the constructor (step 5).