• 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

Doubt in innerclass  RSS feed

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me describe the code i came across.There is an innerclass with private constructor.Then a method in the outerclass instantiates the innerclass and returns the reference.The code compiles fine.
How is this possible?As per my understanding a class with a private constructor can be instantiated only within that class.Am i missing out something?help me...
Thanks,
vinu.
 
vinuharan haran
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let me include the code...

it compiles fine and prints I'm private constructor
 
stable boy
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Outer class is the enclosing top-level class of the member class inner, the class inner belongs to the outer class and as such the outer class has all rights to create a new instance of the inner class even when the constructor is private.

I have done a write up on member classes which might help you understand all concepts of member classes, you can find the article here
[ April 05, 2005: Message edited by: Thomas De Vos ]
 
vinuharan haran
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for ur reply.
So an outerclass can access the innerclass private members through the innerclass instance.

The result is I'm private constructor
10
Normally the private members are not accessible outside the class.why is it different for an inner class?
 
vinuharan haran
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be precise why is the inner class behaviour different from other (non-nested) class?
thanks,
vinu.
 
Thomas De Vos
stable boy
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooops, I wasn't clear enough

The exact definition of an inner class means that it is a "member" of your enclosing class and therefore falls in *scope* of the enclosing class, in your case this is the top-level class.

Just the same way a class can have member variables and methods which can be defined as private.

There is a very close relationship between the member class and the enclosing class.
 
vinuharan haran
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it.Thanks..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!