• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface variables

 
karimkhan pathan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


output :
10
j=3
jj=4
3

Can you tell me why jj=4 is printing when i call K.j
 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then why is not printing when we invoke J.i(First SOP)
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, isn't that tricky question which need the "thorough" knowledge of JLS.
 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, Java "compile-time constant" invokes some corner case.

Moreover, I think referring JLS gives the most clear answer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic