This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Interface variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface variables" Watch "Interface variables" New topic
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: 2902
    
    1

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: 2902
    
    1

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface variables
 
Similar Threads
Question about Interfaces..
Interface question
Interface Q
Question from Enthuware
Initialization Related Prob