0

I am unable to understand why 'y' is printed prior to 'c'... Am I missing something here.. Kindly help

public class Sequence {

    Sequence() {
        System.out.print("c ");
    }

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

    public static void main(String[] args) {
        new Sequence().go();
    }

    void go() {
        System.out.print("g ");
    }

    static {
        System.out.print("x ");
    }
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332

4 Answers4

1

The output is

x y c g

In java, the order of execution is as follows.

  1. Static block is invoked when the class is loaded - prints x
  2. Initializer block is called when a new instance is created(just before constructor executes) - prints y
  3. Constructor - prints c

Then you have called the method go() that prints g.

Suppose Sequence inherits from SuperSequence.

public class SuperSequence() {
    static {
        System.out.print("staic of super ");
    }
    public SuperSequnce() {
        System.out.print("constructor of super ");
    }
}

The output will be

static of super x constructor of super y c g

Because the static block of super class is executed first. Then the static block of Sequence class.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Suganthi
  • 417
  • 3
  • 14
  • 1
    It's not *nameless block* is called *initialization block* and the definition is to be executed after super class constructor and before the class constructor. – Luiggi Mendoza Jun 09 '13 at 05:23
  • My bad. Initialization block. I was trying hard to recollect its name. Thanks for correcting :) – Suganthi Jun 09 '13 at 05:25
  • IMO it would be great if you could show in your answer that super class constructor is called even before the initialization block and provide an example (note that this is not explained anywhere in the proposed duplicated Q/As). – Luiggi Mendoza Jun 09 '13 at 05:27
  • I get your point. But Which is the super class here, in the question? – Suganthi Jun 09 '13 at 05:30
  • Is nowhere but it would be something nobody addressed before (thus making a huge difference among other provided answers). Again, this is just my opinion :). – Luiggi Mendoza Jun 09 '13 at 05:31
  • Ah! Great idea. I totally agree! – Suganthi Jun 09 '13 at 05:34
1

Below clarification should help: (Execution in order)

  • static {}: This block is executed when class is loaded. This is called as static initialization block.
  • {}: This block is executed every time a new instance of the class is created. This is called instance initialization block.
  • Sequence(): After the above block, constructor is executed on new instance creation.
ajay.patel
  • 1,957
  • 12
  • 15
0

In java static block always execute first, then followed by initialization block which is written as
{
//...your code to initialize any variable
}

remaining two block like constructor Sequence() and void go(); method will execute accordingly.
As per official documentation of java about initialization block is:

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

have a look on this oracle documentation link.

Aman Gupta
  • 5,548
  • 10
  • 52
  • 88
0

I changed the source code of question to add a super class and a static block in super class. Below code prints:

test static x zy c g

Code:

  class test{

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

    public test(){
        System.out.print("z");
    }
  }

  class Sequence extends test { 

    Sequence(){
        System.out.print("c ");
    }

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

    public static void main(String[] args) { 

        new Sequence().go(); 
    } 

    void go(){
        System.out.print("g "); 
        } 

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

So it works exactly as Luiggi Mendoza said:

  1. Static block of Super class.
  2. Static block of sub class.
  3. Superclass constructor.
  4. Initializer block.
  5. Subclass constructor.
Lokesh
  • 7,810
  • 6
  • 48
  • 78