• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to access static nested class w/o instance of outer class

 
Vishy Karl
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
The foll. statement is true
"Static nested class can be accessed without having an instance of the outer class".

Now my question is look at the following code.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

System.out.println(Bigouter.Nested()); //Then why is this not OK ??

You're referring to Nested as if it's a static method whereas a new operator is expected. Wrong syntax.
 
Vishy Karl
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vad for your reply
But what you have said is exactly my doubt. Why does the compiler complain if we try to access the static innner class just as a static method ??
Why do we need the syntax of
new Bigouter.Nested() ??
And as we are using this syntax we are in fact creating an instance of the outer class named Bigouter and then accessing the Nested class.
Then how is the statement

"Static nested class can be accessed without the instance of the outer class " true ??

Could someone plz. throw light on this ?? i am really confused
Thanks,
 
Sunder Ganapathy
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer to JLS( 8.5), given below .
If a member class or interface declared with simple name C is directly
enclosed within the declaration of a class with fully qualified name N, then the member class or interface has the fully qualified name N.C.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not create a new instance of the outer class with this:
new Bigouter.Nested();
In fact, an instance of the inner class is created. The inner class in your code is a top-level nested class having no special relationship with the enclosing class other than namespacing resolution. So, the fully qualified name of the inner class is Bigouter.Nested.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic