Hi 'LL.. I came across this code while searching for inner classes in javaranch. class Test{ Test caller(boolean b){ class inner_c extends Test{ int i = 5; void printval(){ System.out.println("initialized inner i " + i); } } inner_c c = new inner_c(); return c; } void printval() { System.out.println("Inside Test1.printval()"); } public static void main(String args[]){ Test t = new Test(); Test temp = t.caller(false); temp.printval(); } } as per my knowledge inner_c is an local class defined in a method caller. then temp is able to access printval() in inner_c(Not In Test class) as it is out of scope in the main methiod thnx rishi
Hi Rishi I don't really know whether I understood your question fully or not. Well here's my take at it. Test caller(boolean b) returns a Test type of object which in this case is an instance of class inner_c. So basically you can replace
with
[ August 05, 2003: Message edited by: Anupam Sinha ]
Hi Rishi, The class inner_c, although a local class, can extend the Test class. So when it defines the method printval(), it overrides Test's printval() method. And when the call temp.printval() is made, polymorphism will still be at work. The method to invoked will be resolved a run-time, and in this case, it will resolve to inner_c's printval(). Hope this helps. [ August 05, 2003: Message edited by: Alton Hernandez ]
Rishi Wright
Ranch Hand
Joined: Jun 25, 2003
Posts: 46
posted
0
Ok Alton ..that means if u extend any outer class(or any class outside that method )then local class will be still available as a subclass even we exit the method..is it so?? is this same for anonymus class also? thnx rishi
Alton Hernandez
Ranch Hand
Joined: May 30, 2003
Posts: 443
posted
0
Hi Rishi, A little modification on the code will show you that this is also true for anonymous class:
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.