aspose file tools*
The moose likes Beginning Java and the fly likes Inner Class question.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner Class question.." Watch "Inner Class question.." New topic
Author

Inner Class question..

Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
Here is some Code:



My question is that when I put the Class : MyInner in another file in the same package, then why doesnt it get recognized in the Class MyOuter? I have made the font bold where it throws error.
[ August 14, 2008: Message edited by: Somnath Paul ]

Preparing for SCJP 6.0
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

you can not create instance of innerclass from different class(other than outer class)
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
Originally posted by seetharaman venkatasamy:
you can not create instance of innerclass from different class(other than outer class)


But I am creating the instance of inner class in the MyOuter only !



And since the access is default package, it should recognize the inner class..
Am I wrong? Please correct me if I am, I am very new to Java.

[ August 14, 2008: Message edited by: Somnath Paul ]
[ August 14, 2008: Message edited by: Somnath Paul ]
Richard Bradford
Ranch Hand

Joined: Apr 20, 2004
Posts: 48
Once you move the MyInner class to another file it is no longer an inner class of MyOuter. You would have to use it like so:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

Originally posted by seetharaman venkatasamy:
you can not create instance of innerclass from different class(other than outer class)

Yes you can, as long as a) it's visible, and b) you have a reference to an enclosing outer class.

Somnath,

the problem in your code is that MyInner is NOT an inner class - it's a toplevel class instead. If you want it to be an inner class, you should put the entire class definition inside the class definition of MyOuter. Basically, just move the last } of your outer class to after the inner class (and then fix indentation).

You might want to know that, from an inner class, you can get a reference to its outer class by calling MyOuter.this
[ August 14, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner Class question..
 
Similar Threads
Regular Inner class
why private member variable of inner class can't be accessed by its outer class but mehod can be?
why private member variable of inner class can't be accessed by its outer class but mehod can be?
inner class instantiation from outside the the outer class
Inner class