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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Inner classes" Watch "Question on Inner classes" New topic
Author

Question on Inner classes

K Raj Kumar
Greenhorn

Joined: Sep 16, 2008
Posts: 17
Hi Ranchers!

[code]
class A
{
void m(){ System.out.println("outer");}
}
public class TestInners{
public static void main(String [] args){
new TestInners().go();
}
void go()
{
new A().m();
class A
{
void m(){ System.out.println("inner");}
}
Class A
{
void m(){ System.out.println("middle");}
}

Output:middle
Source: SCJP6 Study guide, Kathy sierra and Bert bates, Selftest, Inner classes, page 688.
Help needed.Thanks in advance!


With Regards,<br />K.Raj Kumar.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952

void go()
{
new A().m();
class A
{
void m(){ System.out.println("inner");
}
}


Here class A is declare after new A().m(), so it will not find "inner".



Here inner class A shadows the outer class A, so "middle" will be output.


SCJP 6
Sushant Kaushik
Greenhorn

Joined: Dec 12, 2008
Posts: 25
@Punit

Here inner class A shadows the outer class A, so "middle" will be output.


You mean to say middle class A shadows the outer class A, as inner class is not visible when new A().m()was called, Correct?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
as inner class is not visible when new A().m()was called, Correct?


Method local inner class is not visible when new A().m() is called.
Sushant Kaushik
Greenhorn

Joined: Dec 12, 2008
Posts: 25
Got it..Thanks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

I think few closing curly braces (}) are missing in the code :roll: ...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi Shushant,

could you post the correct code again.('}' are missing it seems)

Thanks
Preetha
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Watching the output, this would be the correct code

Sushant Kaushik
Greenhorn

Joined: Dec 12, 2008
Posts: 25
Thanks Ankit. Just to add, if I put the inner A before calling go() then it will call inner A as this inner class is visible at Line 1.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Inner classes