Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner Class

 
Smitha Ballikar
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ryan Kade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the answers have to be C and D.

A and B are both wrong for the same reason: InnerClass doesn't exist statically, so there's no way to instantiate it independently of OuterClass. If you stick lines A and B inside a non-static method of OuterClass, then they would work.

E is wrong because it uses this, which is not available in a static context.

C and D both create instances of OuterClass before trying to create an InnerClass.
[ August 24, 2005: Message edited by: Ryan Kade ]
 
Bhaskar Dabbigodla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer C is OK but I am surprised how D is correct. Is it not required to represent the InnerClass with OuterClass.InnerClass i = o.new InnerClass() notation.
 
Aakash Parashar
Ranch Hand
Posts: 123
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhaskar,

Constructor of Innerclass is invoked from main which is a static method. So, without creating instance of Outerclass that constructior cannot be accessed from main method.
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This one confused me a bit so I tried to find as many ways as I could think of instantiating an inner class and tried them in both a static and non-static context. These were my results:



Hope that is helpful.
Josh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic