Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding Modifiers for Inner classes

 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Mike Simmons
Ranch Hand
Posts: 3041
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

Another question is a nested class declared public but not static. Does it make sense??
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.

All nested classes are static. Have you see the Java™ Tutorials section?
 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3041
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3041
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry; I was mistaken about the nomenclature.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic