The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


If the facts don't fit the theory, change the facts...

Posted by Kishori Sharan on November 17, 2000 at 9:50 AM

Bruce Eckel is wrong is saying that, "If you want to make an object of the inner class anywhere except from within a non-static method of the outer class, you must specify the type of that object as OuterClassName.InnerClassName, as seen in main( )."
He should have rephrased the sentence as saying , "..., you should ( not must ) specify the type of that object as OuterClassName.InnerClassName for better readability and clarity and to make a point that the object you are creating is an object of an inner class which is defined within Outer class , as seen in main( )."

When you are writing this code then
Inner c = new Outer().new Inner();
"Inner c" is just saying that "c" is an object reference of class "Inner". Since class "Inner" is defined in Outer class so its data type is always availabel within that class ( within static as well as non-static methods ) . What is more important is you cannot have an object of a non-static inner class without having an object of outer class which is clear in "new Outer().new Inner()". However, "Outer.Inner c" is necessary when you want to create an Object of Inner class in another class ( i.e. not in any method of Outer class ) .



Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us