my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to access static nested class w/o instance of outer class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how to access static nested class w/o instance of outer class" Watch "how to access static nested class w/o instance of outer class" New topic
Author

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

Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
Posts: 116
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.


"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504

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

Joined: Sep 08, 2003
Posts: 116
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

Joined: Apr 01, 2003
Posts: 120
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

Joined: Aug 25, 2003
Posts: 504
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to access static nested class w/o instance of outer class
 
Similar Threads
class types
inner classes
Confused with points in Kathy's book
Innerclasses
Outer instance for static nested class