• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

interfaces

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 child proofed my house but they still get in. Distract them with this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic