Java OCA OCP Practice Question 2356

Question

What is the output of the following code?

enum Light {//from  www  .j  a v a 2 s.com
    RED;
    static {
        System.out.println("Static init");
    }
    {
        System.out.println("Init block");
    }
    Light(){
        System.out.println("Constructor");
    }
    public static void main(String args[]) {
        Light red = Light.RED;
    }
}
a  Init block//w  w  w. j a v a  2  s .  co  m
   Constructor
   Static init

b  Static init
   Init block
   Constructor

c  Static init
   Constructor
   Init block

d  Constructor
   Init block
   Static init


a

Note

The creation of enum constants happens in a static initializer block, before the execution of the rest of the code defined in the static block.

Here's the decompiled code for enum Light, which shows how enum constants are initialized in the static block.

To initialize an enum constant, its constructor is called.




PreviousNext

Related