Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JQ+-- INTERFACES  RSS feed

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Chk this long yet interseting piece of code:-
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);//1
System.out.println(K.j);//2
}
public static int out(String s, int i)
{
System.out.println(s + "=" + i);
return i;
}
}
What will be the output when class Test is run ?
Line 1 gives the output :- 1
Line 2 gives the output :-j=3
jj=4
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,
Kaushik
 
kaushik banerjee
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
Regards,
Kaushik
 
Enthuware Software Support
Rancher
Posts: 4175
34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A detailed explanation right from the source is here: http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44557

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
Try out the world's only WebCompiler!
www.jdiscuss.com
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my shot
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
System.out.println(J.i);//1
System.out.println(K.j);//2
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
James
 
kaushik banerjee
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,
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.
Regards,
kaushik
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every field declaration in the body of an interface is implicitly public, static, and final
 
James Du
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jeena's right, Every field declaration in the body of an interface is implicitly public, static, and final
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.
Regards,
James
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!