Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Inner class

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

-Sanjana
 
reply
    Bookmark Topic Watch Topic
  • New Topic