I have inner class in my code. I want to give public access to its instances, but only outer class should be able to create this instances, like in "private" access. Is it possible without making properly small package (or creating public interface for every such inner class)?
(Sorry if my english is bad :P)
It is possible. Declare your inner class public, but its constructor private. This way you can create it only inside your enclosing class and itself, but not from outside.
By default,If you want to get the instance of the inner class you need to have the Outer class first.
A inner class is a member of its enclosing class.
You need not to do anything for that.
Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private
I hope I understood your question in right way.
Please refer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With