What will be the output when class Test is run ? Answer is It will print j = 3, jj=4 and then 3. Does anyone know why is it printing jj=4 too?? Edited by Corey McGlone: Added CODE Tags [ March 08, 2004: Message edited by: Corey McGlone ]
Hi Jay, Check out the JLS chapter "12.4 Initialization of Classes and Interfaces". I will do that myself as well. It explains your problem. Best regards, Gian Franco Casula [ March 08, 2004: Message edited by: Gian Franco Casula ]
The JLS stands for Java Language Specification and is the ultimate source for all Java related questions (at least concerning the exam). You can find the entire specification online here.
I found the answer "The reference to J.i is to a field that is a compile-time constant; therefore, it does not cause I to be initialized. The reference to K.j is a reference to a field actually declared in interface J that is not a compile-time constant; this causes initialization of the fields of interface J, but not those of its superinterface I, nor those of interface K. Despite the fact that the name K is used to refer to field j of interface J, interface K is not initialized." Thanks Corey and Gian [ March 08, 2004: Message edited by: Jay Patel ]
I'm glad you found it, Jay. I was hoping you'd take the initiative to hunt through the JLS for the answer - get used to using the JLS. The SCJP exam is based on it so, if you can find your way around it, you're much better off.