This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interfaces" Watch "interfaces" New topic
Author

interfaces

Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
Hi,

Can you please explain this code


I think,in interface the variables must be initialized at thier declaration.But here the code is containing method calls.And also iam not able to understand the output.

Please Help Me


Thanks,
Scjp1.4
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Hi Above,

Try to check your code. I guess you missed out the closing brabes for public static void main. I have modified the code as,



It gives me the O/P,


run-single:
j=3
jj=4
3
BUILD SUCCESSFUL (total time: 0 seconds)

I don't understand why it is not printing out the vakues for i and k from the out method???

Lakshmi as per your quote,
I think,in interface the variables must be initialized at thier declaration.But here the code is containing method calls


I also have the same doubt as in interface all variables are implicitly public static final. So in the above code, the call ii = Main.out("ii", 2) assigns ii the vakue of 2 only after the method returns the value. So why does this not produce any compile time errors??

Anyone please explain?

Regards,
Jothi Shankar Kumar. S


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Tilo Hemp
Ranch Hand

Joined: Nov 21, 2005
Posts: 91
Obviously, the fields of ancestors of an interface are only initialized when the corresponding interface they are declared in is accessed first (and not, how it would be expected for classes, when the child interface is loaded).

To make it clearer: when a static field of a class is first accessed, all ancestor classes are loaded recursively, including the initialization of the respective static fields.

On the other hand, when a (always static) field of an interface is first accessed, the fields of it's ancestors are not initialized. Only it's own fields are initialized.

The following code demonstrates this:

wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
In 12.4.1 When Initialization Occurs:

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.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Hi Tilo Hemp,

Thanks for your explanation. I'm not quite understanding the flow of output. I tried it on my system and I fail to understand. Can you please explain me?

Regards,
Jothi Shankar Kumar. S
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interfaces
 
Similar Threads
Interface Q
Question from Enthuware
Interface question
Interface variables
Initialization Related Prob