Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Inner class question in Sahir Shah's Mock Exam  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Sahir Shah's Mock Exam
http://www.geocities.com/sahirshah/applets/mocktest.html
III
1. Consider the following code
public class Outer{
class Inner{}
public void methodA()
{
/////
}
}
What are the valid ways to create an instance of the inner class from methodA ?
1. Inner i = new Inner();
2. Outer.Inner i = new Inner();
3. Outer.Inner i = new Outer.Inner();
4. Inner i = new Outer.Inner();
5. Outer.Inner i = new Outer().new Inner();
6 Object o = new Outer().new Inner();
The answer given is 1, 2, 5, 6
but when run in JDK, all are valid statement to create an instance of inner class.
My code is like this:
class Outer
{
class Inner {int a = 2;}

public void aMethod(){
Inner i = new Inner(); // this.Inner i = new this.Inner()
System.err.println(i.a);
Outer.Inner i2 = new Inner();
System.err.println(i2.a);
Outer.Inner i3 = new Outer.Inner();
System.err.println(i3.a);
Inner i4 = new Outer.Inner();
System.err.println(i4.a);

Outer.Inner i5 = new Outer().new Inner();
System.err.println(i5.a);

Object o = new Outer().new Inner();
System.err.println(((Inner) o).a);
}
public static void main(String[] args){
Outer o = new Outer();
o.aMethod();
}
}
Can somebody give me some clue?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!