• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question related to Interfaces

 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question VI: (Source: Enthuware)




Can anyone please explain the reasoning behind the output of this code?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because, the variables are implicitly static, so, they get initialized when the interface is loaded.
 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i am not understanding the output over here ..


output is

j=3
jj=4
3

what i think it should be is

ii=2 (because J extends I so it also should be loaded....)
j=3
jj=4
3


anyone please explain this ...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, the compiler will optimize the usage.
 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still not getting ..
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone please explain this?
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not getting it..
 
Pradeep Kr
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really need to know the sequence of Class Load for this exam? I guess No.
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to answer such questions then?
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep Kr wrote:Do you really need to know the sequence of Class Load for this exam? I guess No.


it'll be much better if you do
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ankur trapasiya wrote:well i am not understanding the output over here ..


output is

j=3
jj=4
3

what i think it should be is

ii=2 (because J extends I so it also should be loaded....)
j=3
jj=4
3


anyone please explain this ...


If you dive into the JLS, you can get the answer. Here is the specification,

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. So, the super interfaces of the interface K won't be initialized when the interface K is initialized. Hence only the static fields of that interface will be initialized. So, we have that results.
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was a great help. Thanks you so much Abimaran.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hrishikesh Yeshwant Alshi wrote:This was a great help. Thanks you so much Abimaran.


You are Welcome!
 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Abimaran for such a nice explanation... i got the point
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ankur trapasiya wrote:thanks Abimaran for such a nice explanation... i got the point

You are Welcome!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic