File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class load order for Inner class

 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, This is from K&B. On what basis Line 13 prints "middle".
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic