| Author |
Interface variables
|
karimkhan pathan
Ranch Hand
Joined: Jul 14, 2008
Posts: 86
|
|
output :
10
j=3
jj=4
3
Can you tell me why jj=4 is printing when i call K.j
|
karim
|
 |
Bupjae Lee
Ranch Hand
Joined: May 14, 2007
Posts: 107
|
|
I think this example is from this part of Java Language Specification
I recommand you visit and read this link.
In short: K.j actually is J.j and trying to use J.j invokes iniitialization of interface J.
During initialization, both J.j and J.jj should be initialized, so both j=3 and jj=4 are printed.
|
 |
karimkhan pathan
Ranch Hand
Joined: Jul 14, 2008
Posts: 86
|
|
|
Then why is not printing when we invoke J.i(First SOP)
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
karimkhan pathan wrote:Then why is not printing when we invoke J.i(First SOP)
JLS wrote:The reference to J.i is to a field that is a compile-time constant; therefore, it does not cause I to be initialized.
Quote from the same link.
|
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
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.
enough information . Study twice,if you not understand .
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
But, isn't that tricky question which need the "thorough" knowledge of JLS.
|
 |
Bupjae Lee
Ranch Hand
Joined: May 14, 2007
Posts: 107
|
|
In my opinion, Java "compile-time constant" invokes some corner case.
Moreover, I think referring JLS gives the most clear answer.
|
 |
 |
|
|
subject: Interface variables
|
|
|