Consider the following code:
class Super { static String ID = "myId"; } class Sub extends Super{ static { System.out.print ("In Sub"); } } public class Main{ public static void main (String [] args){ System.out.println (Sub .ID); } /* w ww . ja v a2 s . co m*/ }
What will be the output when class Main is run?
Select 1 option
Correct Option is : B
A class or interface type T will be initialized at its first active use, which occurs if:
T is a class and a method actually declared in T is invoked.
T is a class and a constructor for class T is invoked, or U is an array with element type T, and an array of type U is created.
A non-constant field declared in T is used or assigned.
A constant field is one that is both final and static.
That is initialized with the value of a compile-time constant expression.