This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Hi, this is a question from a mock exam. If inner - refers to a static inner class outer - refers to the enclosing class which of the following statements are true? 1.Outer outerObj = new Outer() ; Outer.Inner InnerObj1 = outerObj.new Inner() ; 2. Outer.Inner InnerObj2 = new Outer.Inner() ; 3.Outer.Inner InnerObj3 = new Outer().new Inner() ; given ans:1,2 My answer is only 2 because according to me there is no difference between 1 and 2. Any comments would be appreciated.
Option 1 shouldn't even compile because the syntax is plain wrong.
An instance of a static inner class cannot be created using an instance of its enclosing class. Top level nested classes maintain no special relationship between their enclosing classes, it's slightly more than namespacing resolution. By convention, the full qualified name of the class is used to instantiate a static inner class outside of its outer class.
Joined: Nov 25, 2003
Yes Vad Fogel. I agree with you. That's why i wanted to reconfirm. Thanks for your reply.