Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion over inner classes

 
Gaia Nathan
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gaia Nathan
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Colin, I get it now.
Gaia
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic