aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class load order for Inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Class load order for Inner class" Watch "Class load order for Inner class" New topic
Author

Class load order for Inner class

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, This is from K&B. On what basis Line 13 prints "middle".
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

in your code
there are three Test classes
first is with package scope
another that created in the public class is inner class for P688_Question08_Ask
and another is method local inner class in the main method

now on line 13, the class Test is instantiated but that class is not the method local inner class (Remember that method local inner class cannot be instantiated before creating it)
so the call
new Test().test();
checks for the class that is regular inner class or another simple class
in this case we have both
but nearest scope is for the regular inner class in public class P688_Question08_Ask
so line 13 invocation prints "middle"

line 19 instantiation and invocation prints inner because the METHOD LOCAL INNER CLASS is instantiated now as we are instantiating it AFTER class declaration

now
to verify this
just comment out the regular inner class i.e. line 22 to 26
now
line 13 invocation will cause outer class Test to get instantiated as inner class is not available now
and the output will change to

outer
inner



hope this helps

SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Thanks Prasad. Good explanation. One final question is, How class Test at Line 22 takes higher precedence(nearest execution spot) over class Test at at Line 1..What is that concept ?
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

class at line 22 is inner class isn't it?
that means
the scope of class at line 22 is local scope
and for the outer class declared at line 1
it is package scope
hence the higher precedence
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class load order for Inner class