• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static vs non-static nested class  RSS feed

 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I’m trying to understand how to decide when to make a nested class static or non-static. These are my assumptions. Please correct me if I’m wrong.
1) Make a nested class static if each instance of its enclosing class may have one or more instances of its nested class, for example, a HashMap has a static HashMap.Entry nested class because each HashMap instance may have one or more HashMap.Entry instances
2) Make a nested class non-static if each instance of its enclosing class must have only one instance of its nested class, for example, an AbstractButton has a non-static AbstractButton.Handler nested class because each AbstractButton instance must have only one AbstractButton.Handler instance.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Static nested classes, or just nested classes, operate without the need to have an outer class. Non static nested classes, or correctly called inner classes, require an outer class, and has access to it. With inner classes, it is not possible to be created, without an outer class.

So, technically, the distinction of having the outer class having "one or more" or "only one" doesn't make sense. What you are describing is merely a few use cases that you examined.

Henry
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
technically, the distinction of having the outer class having "one or more" or "only one" doesn't make sense

Thanks for your reply. Isn't it true that a one to one relationship exists between an inner class instance and an outer class instance?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Bishara wrote:
Thanks for your reply. Isn't it true that a one to one relationship exists between an inner class instance and an outer class instance?


I guess that depends on how you look at it.

Yes, an inner class can only have one outer class instance. However, you can create as many inner classes as you want using the same outer class instance.

Henry
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can declare as many inner classes as you want inside the same outer class but I'm talking about the relationship between instances. Correct me if I'm wrong but I think a one to one relationship exists between an outer class instance and an inner class instance (i.e. an inner class instance exists in the context of one outer class instance). And a one to one or one to many relationship exists between an outer class instance and static nested class instances (i.e. an outer class instance may use one or more static nested class instances).
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Bishara wrote:Correct me if I'm wrong but I think a one to one relationship exists between an outer class instance and an inner class instance


Not true. It is not one-to-one as an outer class can have any number of inner class instances.

Joe Bishara wrote:And a one to one or one to many relationship exists between an outer class instance and static nested class instances


Also not true. An inner static class does not even need an outer class instance to be initialized. So there is no relation at all.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:an outer class can have any number of inner class instances

Of course an outer class can have any number of inner class instances, but one inner class instance exists in the context of one outer class instance.

Paweł Baczyński wrote:there is no relation at all

I think the reason for declaring a static nested class inside another class is because there is some kind of relationship between the two classes i.e. when a static nested class B is declared inside class A, the objective is that you want an instance of class A to use (communicate with) one or more instances of class B
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be fair, you are stretching the definitions here.

* In general, when we talk about a "one to one relationship", it is in *both* directions. Your definition is from the inner class to the outer class only. That is like saying that polygamy marriages are "one to one" because each wife only has the one relationship with the one husband.

* In general, when we talk about a "one to many relationship", we mean that the relationship exists. In this case, since it is not possible to determine which outer class instance belongs to which inner class instance, it is argued as not having a relationship at all.


Having said that, yes, it is your definition (as this terminology doesn't really exist for what you are describing). However, since your definition doesn't seem to follow norms, this debate isn't that useful.

In other words, if this definition helps you "remember" the difference better, then ... great. But your original post asked for confirmation (whether you are wrong), which we can't really do (as we disagree on your definition).

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!