GeeCON Prague 2014*
The moose likes Java in General and the fly likes Understanding Modifiers for Inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Understanding Modifiers for Inner classes" Watch "Understanding Modifiers for Inner classes" New topic
Author

Understanding Modifiers for Inner classes

marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Hi,

Javadoc :
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.


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?

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?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18875
    
  40

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?


You use the private modifier when you don't want the inner class instantiated (or used) outside of any constructor, initializer, or method of the outer class. I am pretty sure that there are use cases where this is necessary.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18875
    
  40

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?


A nested class is like a top level class, in that you don't need an instance of an outer class to instantiate one. It is however, not a top level class. If you don't specify it as public, it is not "visible from outside".

Henry
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Thanks.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
You may have read something that talks about static nested classes as "top-level nested classes". In the distant past, this was Sun's official term for them. Static nested classes were officially described as being "top-level". This was a profoundly stupid and confusing idea, and Sun abandoned the practice over ten years ago. Nowadays "top-level" means only top-level, classes not nested inside anything, hence the term "top". Unfortunately, some books still exist which use the confusing term "top-level nested class". Please ignore such books.
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Thanks.

Another question is a nested class declared public but not static. Does it make sense??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39070
    
  23
No.

All nested classes are static. Have you see the Java™ Tutorials section?
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Hi thanks for your input. Of course I read the tutorials before posting.

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.



Here's the link http://developer.android.com/reference/android/app/Service.html, Local Service Sample section

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18875
    
  40

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.


A "nested" class is also referred to as a "static inner" class. A non-static inner class is just referred to as an inner class. Confusing? Yes. Heck, even I use those terms interchangably sometimes.

As for "what it does mean", it means that to create an inner class, you need to have an instance of the outer class... which is not needed for nested (aka static inner) classes.

Henry
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
Campbell Ritchie wrote:No.

All nested classes are static. Have you see the Java™ Tutorials section?

Um, if you read what's written at that link, I think you will see that nested classes include both static and non-static classes. Also local and anonymous classes. This is also the usage employed in the Java Language Specification. Joe Darcy's blog gives a nice explanation of how the terms are used.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
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:
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
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:


This helped thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39070
    
  23
Sorry; I was mistaken about the nomenclature.
 
GeeCON Prague 2014
 
subject: Understanding Modifiers for Inner classes