• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Reg. Interface intialization

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<quote>
interface I {
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);
}
class Test {
public static void main(String[] args) {
System.out.println(J.i);
System.out.println(K.j);
}
static int out(String s, int i) {
System.out.println(s + "=" + i);
return i;
}
}
produces the output:
1
j=3
jj=4
3

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.
</Quote>
Above is an example from JLS, i am clear about J.i
but the K.j portion i am not able to understand as to
how j is non-static ?
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Angela -
If you run this program with the -verbose option, you'll see your analysis confirmed:

Since every element of the code above is static, the run-time doesn't need to load K. When the compiler sees the reference to K.j it knows that the value has nothing to do with K at all, and replaces it with an inline reference to the superinterface.
You can see this by running javap -c against each of the resulting classes and tracing the bytecode. It's not an easy thing to read for the first time, but that's where the proof is.
Here's an example from your code:
$ javap -c Test
<br /> ------------------<br /> Michael Ernest, co-author of: <A HREF="http://www.amazon.com/exec/obidos/ASIN/0782128254/electricporkchop" TARGET=_blank rel="nofollow">The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited September 04, 2001).]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both for the marvellous question and answer.
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Micheal,
I could catch hold of bit of the javap output.Thanks a lot.
( Reminded me of my microprocessor subject !! )
I have one more doubt as follows :


Every variable declared in an interface is by default
public, final and static( compile-time constant ).


Fine. Now in case of interface I, the variable i is compile-time constant.
But in case of interface J, why are variable j and jj not
compile-time constant, or for that matter even ii or k variables !
I suppose they should static, public and final also because i cannot
do something like the below in the main method :
K.j = 90;
Test.java:19: cannot assign a value to final variable j
K.j = 90;
^
1 error
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela you are right
In JLS 15.28 you will find the expressions that are considered to be constant
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic