• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Inner-class Confusion

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
This is a code from javachamps about inner-classes.


It asked about which instantiations are correct. But in instantiation 1 and 3, can we qualify an inner class without its
outer-class name. I am confused. Please help.

Arhaan
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you ran the code?
 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Stephan,
It worked well for the first three instantiations. I was just wondering. Ofcourse compiler allows it means we can do that.
But i was just asking, how come Engine is allowed without its parent name.
Arhaan
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In first and second instantiation you had used new new AirJet().new Engine();


and in third instantiation you had first created object of Airjet and using that object you instantiated as

So here is also works.
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And one more thing:

Engine engine1 = new AirJet().new Engine();

and

AirJet airjet = new AirJet();
Engine engine1 = airjet.new Engine();

are equivalent.
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arhaan Singhania wrote:Hello Stephan,
It worked well for the first three instantiations. I was just wondering. Ofcourse compiler allows it means we can do that.
But i was just asking, how come Engine is allowed without its parent name.
Arhaan


Because of the scope. You are using the Engine identifier from the main method inside the AirJet class. Since the scope is already inside AirJet, it assumes you are referring to the inner class Engine. You would not be able to do this if you used Engine outside the AirJet class, then you would have to use AirJet.Engine instead.
 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Stephen/Pramod,

Thankyou very much. I have understood the concept very well and Stephen the scoping explanation was awesome.
Thanks a ton.

Arhaan
 
reply
    Bookmark Topic Watch Topic
  • New Topic