Modifiers
You can use the same modifiers for inner classes that you use for other members of the outer class. For example, you can use the access specifiers — private, public, and protected — to restrict access to inner classes, just as you do to other class members.
marwen Bakkar wrote:
An inner class is instanciated within the context of an object of the outer class in which this inner class is declared,
and like private member fields, is not visible to the world. So when would be the private modifier used with an inner class?
marwen Bakkar wrote:
Also, to make an inner class a top-level one and visible from outside, we use the static modier. What's the use
of the public modifier then?
marwen Bakkar wrote:
Actually I was reading an Android API where I found a public but non static class declaration, and I was wondering what it does mean.
Campbell Ritchie wrote:No.
All nested classes are static. Have you see the Java™ Tutorials section?
marwen Bakkar wrote:Another question is a nested class declared public but not static. Does it make sense??
Mike Simmons wrote:
marwen Bakkar wrote:Another question is a nested class declared public but not static. Does it make sense??
Yes. Public means it's possible to "see" the class and use it anywhere - but a non-static nested class needs an instance of the enclosing class to function. If you're writing code outside that class, you don't have an implicit "this" reference to the outer class, so instead you must use an explicit reference, like this:
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |