• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in innerclass  RSS feed

 
vinuharan haran
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
 
Thomas De Vos
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..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!