• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Confusion over inner classes

 
Ranch Hand
Posts: 62
  • 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.
 
Ranch Hand
Posts: 30
  • 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.
 
Sheriff
Posts: 9099
12
  • 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
  • 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.
 
Ranch Hand
Posts: 88
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Colin, I get it now.
Gaia
 
What's brown and sticky? ... a stick. Or a tiny ad.
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!