Chk this long yet interseting piece of code:-
int i = 1, ii = Test.out("ii", 2);
interface J extends I
int j = Test.out("j", 3), jj = Test.out("jj", 4);
interface K extends J
int k = Test.out("k", 5);
public static void main(String args)
public static int out(String s, int i)
System.out.println(s + "=" + i);
What will be the output when class Test is run ?
Line 1 gives the output :- 1
Line 2 gives the output :-j=3
JQ+ goes onto explain that :-
"A reference to a field is an active use of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface. Initialization of an interface does not, of itself, require initialization of any of its superinterfaces. "
The above explaination is consonance with the output of line 2 but not with that of line 1.However I have checked it out & it gives the specified output. What am i missing ?(Sorry Anil the explaination is not very clear to me especially u'r 2nd last paragraph)
Thx in advance,
somebody pls help me out with the above prob . I am giving the exam on Thursday & am really desperate. Pls don't let me down ranchers.
A special request to the moderators or Paul Anil to help me out before D-Day. I am sure u all can solve my prob.
Looking with big hopes.
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
Your guide to SCJD exam!
Try out the world's only WebCompiler!
I quoted some states in JLS here for convenience
Initialization of a class consists of executing its static initializers and the initializers for static fields declared in the class. Initialization of an interface consists of executing the initializers for fields declared in the interface.
Before a class is initialized, its direct superclass must be initialized, but interfaces implemented by the class need not be initialized. Similarly, the superinterfaces of an interface need not be initialized before the interface is initialized.
A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
T is a class and an instance of T is created.
T is a class and a static method declared by T is invoked.
A static field declared by T is assigned.
A static field declared by T is used and the reference to the field is not a compile-time constant (��15.28). References to compile-time constants must be resolved at compile time to a copy of the compile-time constant value, so uses of such a field never cause initialization.
Invocation of certain reflective methods in class Class and in package java.lang.reflect also causes class or interface initialization. A class or interface will not be initialized under any other circumstance.
A reference to a class field causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface.
Then let's go back to your code
the attempt to use of J.i will get interface J initialized? No, the variable i even not declared in J! then will it get interface I initialized? No also, Notice, through i was declared in I it's a compile-time constant.
the attempt to use of K.j will get interface J initialized? Yes, it was declared in J and it was not a compile-time constant.
Hope it helps, pls. query if there's still doubt
Thx 4 u'r nice explaination . The output seems somewhat clear to me. I think I had this misconception that variables declared in an interface are compile-time constants & therefore the call to the out() method had to be there. However from this piece of code & u'r explaination it seems that interface variables may not be compile time constants ? Do tell me wheter I am correct .Also are all these variables,ie i,ii,j,jj public static & final(That's what Khalid says on Pg189) or only i & j with ii && jj being normal variables.
As for whether the variable is compile time constant, it depends on the variable initializer(if any), in the code mentioned above, only I.i is a compile-time constant, the value could be determined in the compile time, How could you think others are all constant? Do you expect the compiler would run the initializer and get the value? the compiler would never do that.