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

inner class question

 
Ash Gill
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here are few i have


scjp k&b book, chapter 8, self test question 8
inner class doubt


this produces output as middle. i understand that, at line 1, the method local class was not yet defined. But why wasn't the outer class A m() method called instead of inner class A's m() method.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ash Gil wrote:But why wasn't the outer class A m() method called instead of inner class A's m() method.

Hello Ash...I want to extensively answer your question based on my personal observation...

The 'A' inner class (in my opinion) 'overshadows' the 'A' outer class.
In other to instantiate the outer class and invoke the corresponding m() method, the program has to be created inside a package, and you MUST tell java the fully qualified name of the outer class. Here is a modified version of the program, take note of the package declaration and the modification in line 8 of your program.

Output:

HTH

Regards

Ikpefua
 
Ash Gill
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ikpefua,

thanks a lot.
 
Tommy Delson
Ranch Hand
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I've modified the original code to make it works and make sense, if you still have doubt or not clear reread Chapter 8 and check out Java Spec document.

Hope it help....





 
Tommy Delson
Ranch Hand
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ikpefua Jacob-Obinyan wrote:
Ash Gil wrote:But why wasn't the outer class A m() method called instead of inner class A's m() method.

Hello Ash...I want to extensively answer your question based on my personal observation...

The 'A' inner class (in my opinion) 'overshadows' the 'A' outer class.
In other to instantiate the outer class and invoke the corresponding m() method, the program has to be created inside a package, and you MUST tell java the fully qualified name of the outer class. Here is a modified version of the program, take note of the package declaration and the modification in line 8 of your program.

Output:

HTH

Regards

Ikpefua



The modified code doesn't work, its throw an exception. See below...






Let's me try other approach and see if it works...

 
Nerd Herd
Greenhorn
Posts: 1
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this is a really old thread, but there is no solution for the last post.
Tommy Delson wrote:The modified code doesn't work, its throw an exception. See below...

In your current folder, create a folder named "com", and inside that create a folder named "inner_outer". Place the "TestInners.java" file inside the "inner_outer" folder. From your original folder (which contains the "com" folder), you can compile and run the program the following way:
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic