-1
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 "); 
    } 
} 

What is the result?

  • A) c x y g
  • B) c g x y
  • C) x c y g
  • D) x y c g
  • E) y x c g
  • F) y c g x

This is Oracle certification question and answer is option D.I did not understand the answer.I thought option C is correct.Can anyone explain why the answer is option D and not option C?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I suggest you explain *why* you thought option C was right - and then we can explain the flaw in your logic. (Also, please put more effort into formatting your post in future. I've fixed this now, but it was unreadable to start with.) – Jon Skeet Aug 07 '15 at 06:21
  • 1
    take a look [here](http://stackoverflow.com/questions/19058766/java-static-initialization-order) and [here](http://stackoverflow.com/questions/2007666/in-what-order-do-static-initializer-blocks-in-java-run) – SomeJavaGuy Aug 07 '15 at 06:22

4 Answers4

5

Basically, it's just a matter of reading through JLS 12.5 (Creation of New Class Instances) carefully.

In particular, note the order of:

  • Execute the instance initializers and instance variable initializers for this class [...]

  • Execute the rest of the body of this constructor. [ ... ]

y is printed by the instance initializer; c is printed by the body of the constructor - therefore y is printed first.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

The order of execution of java class goes as follows.

  1. Static block initialization because it goes before object creation.
  2. Initialization block of your object
  3. Parent constructor (not listed in your code)
  4. Constructor
  5. Method called afterward
Lemonov
  • 476
  • 4
  • 17
0

You have a static and an instance initializer in this code block.

In sequence, the static initializers are run first, then the instance initializers when an instance of the object is created. This is specified by JLS 12.4.2 and JLS 12.5:

  1. Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.

A new class instance is explicitly created when evaluation of a class instance creation expression (§15.9) causes a class to be instantiated.

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

To break it down a bit more:

  • By virtue of the class being loaded, "x" is printed first.
  • By initializing an instance of Sequence, "y" is printed next.
  • By calling the no-arg constructor of Sequence, "c" is printed third.
  • By invoking the go method, "g" is printed last.
Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230
0

1.static block is executed before main method at the time of classloading.

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

2.The instance initializer block is created when instance of the class is created.

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

3.The instance initializer block is invoked after the parent class constructor is invoked

Sequence(){
System.out.print("c "); 
}
Rafiq
  • 740
  • 1
  • 5
  • 17