aspose file tools*
The moose likes Beginning Java and the fly likes Confusion over inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confusion over inner classes" Watch "Confusion over inner classes" New topic
Author

Confusion over inner classes

Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
Hi All,
To the best of my knowledge, a static inner class does not require an instance of the outer class to exist. Also, the static inner class must be referenced by the name OuterClass.InnerClass
Why then does this code compile?
public class MyOuter {
public static class MyInner {
}

public static void main(String[] args) {
MyInner aMyInner = new MyOuter.MyInner();
}
}
Shouldn't the declaration:
MyInner aMyInner = new MyOuter.MyInner();
generate a compile-time error?
Another question: Can an anonymous class access the member variables and methods of the outer class?
I'd appreciate any replies. Thanks.
Gaia.
Shiva Kumar
Ranch Hand

Joined: Feb 09, 2001
Posts: 30
Hi,
Hey your code is working fine, we can create a static inner class object using outer class name.
I think for Anonymous inner class we can able to call any outer class function, but member variable needs to be final.
Pls correct me if i wrong.
Shiva.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
Just like you can access a static variable with the class name or with an instance of the class, you can access a static nested class with an instance of the outer class or with the outer class name. No error here.

"Can an anonymous class access the member variables and methods of the outer class?"

Yes.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
Thanks Shiva, Marilyn for your replies.
Quoting Shiva:
I think for Anonymous inner class we can able to call any outer class function, but member variable needs to be final.
Yeah, anonymous classes can access both the member variables and methods of the enclosing class but can only access final local variables of the method in which it is declared in.
Phew!
Actually the part I was confused is this:
MyInner aMyInner = .....
Shouldn't it be MyOuter.MyInner instead of just MyInner?
Once again, thanks in advance.
Colin Kenworthy
Ranch Hand

Joined: Aug 06, 2001
Posts: 88
Originally posted by Gaia Nathan:

Actually the part I was confused is this:
MyInner aMyInner = .....
Shouldn't it be MyOuter.MyInner instead of just MyInner?

As it is declared inside the MyOuter class itself, the compiler lets you drop the MyOuter, in fact you can shorten it to just
MyInner aMyInner = new MyInner();
Create a MyOuter2 class which creates a MyInner variable. You will find you have to fully qualify this as MyOuter.MyInner ...
Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
Thanks Colin, I get it now.
Gaia
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion over inner classes
 
Similar Threads
static inner class question
Inner Class reference
inner class reference
(Inner Classes) MyOuter.MyInner vs MyInner
inner class instantiation from outside the the outer class