• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class

 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

-Sanjana
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Vad Fogel. I agree with you. That's why i wanted to reconfirm. Thanks for your reply.

-Sanjana
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic