This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Interface/Class Loading

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
This question is taken from JQ+. There were two question using the same code and asking the same question " What will be the output when class Test is run ? " but the only difference was in the main method. One question had the print statement marked as //1 and the other question had the print statement marked as //2 .




The answer to the above 2 questions are as follows

Que 1) It will print 1 ( System.out.println(J.i) in the main method)
Que 2) it wil print j = 3, jj=4 and then 3 ( System.out.println(K.j) in the main method)

And these are my answers which are wrong

Ans to Que 1 ) It will print ii=2, j = 3, jj=4 and then 1 ( System.out.println(J.i) in the main method)

Ans to Que 2) It will print j=3, jj=4, k=5 and then 3 ( System.out.println(K.j) in the main method)

Interface is loaded when a constant is accessed or assigned to a variable.

For Que 1, since i is defined in Interface K and J extends K, it inherits the variable i. So when J.i is accessed, its super interface will be loaded first and initialized and since all the constants are static and final, they will be initialized in the order they appear. The same loading and initialization will happen for Interface J after Interface K is loaded and Initialized.

For Que 2, I applied the same steps described for Que 1.

My answers were based on the above described understanding which obviously is wrong.

Could some one kindly explain it step by step in detail. My exam is on the 22nd and would really appreciate an early response.

Hope the problem described is clear and not confusing. Thanks in advance.
Kind Regards.
Hasnain Javed Khan.
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hasnain,

As per JLS-:

Initialization of an interface does not, of itself, cause initialization of any of its superinterfaces.



Moreover you can find this link quite helpful from JLS itself as your code with explanation resides there

12.4.1 When Initialization Occurs


thanks
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can some one pls explain this in detail?

Why is this printing
j=3
jj=4 //--> Why is this line getting printed?
3

I was expecting it to print just
j=3
3

Some one pls reply..
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by-Varalakshmi
Why is this printing
j=3
jj=4 //--> Why is this line getting printed?
3

I was expecting it to print just
j=3
3



Hi,
Here as you are calling System.out.println(K.j); // 1 and you can see that J is not a compile time constant. So the super interface will be loaded and initialized , this leads to intializing of super interface of K. thus j and jj of Interface J gets intialized. This puts a call to TestInterface.out() method on both, there by giving j=3,jj=4 also. But this is not the case with System.out.println(J.i); as i is a compile time constant.So for this we just got output for i=1.

Thanks
 
Varalakshmi Ramanarayan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Pranav,

It is clear now..
 
Varalakshmi Ramanarayan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bad of me... I should have seen the link you posted in your previous post before asking the question...
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No probs
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic