aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question related to Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question related to Interfaces" Watch "Question related to Interfaces" New topic
Author

Question related to Interfaces

Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

Question VI: (Source: Enthuware)




Can anyone please explain the reasoning behind the output of this code?


Write your code as if the person who maintains it is a homicidal maniac who knows where you live.
OCPJP6/SCJP6 86%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Because, the variables are implicitly static, so, they get initialized when the interface is loaded.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

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 ...


OCPJP(83%)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

I think, the compiler will optimize the usage.
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

still not getting ..
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

Could anyone please explain this?
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

Not getting it..


No Kaustubh No Fun, Know Kaustubh Know Fun..
Pradeep Kr
Greenhorn

Joined: Feb 17, 2010
Posts: 22
Do you really need to know the sequence of Class Load for this exam? I guess No.
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

How to answer such questions then?
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Dec 06, 2010
Posts: 62

This was a great help. Thanks you so much Abimaran.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Hrishikesh Yeshwant Alshi wrote:This was a great help. Thanks you so much Abimaran.


You are Welcome!
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

thanks Abimaran for such a nice explanation... i got the point
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

ankur trapasiya wrote:thanks Abimaran for such a nice explanation... i got the point

You are Welcome!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question related to Interfaces