If I have for example a class along with a helper class to do some of its functionality, does it make sense to make it as an inner class.
public class F
Here are some uses of inner classes.
So if you have some requirement matching above points than inner classes can be used. It is always better to make inner class private
to prevent access from other classes. In your case use of inner classes is helpful to make code readable and separate logic in the outer class.
As per Oracle Docs, Simply Explained
Compelling reasons for using nested classes include the following:
It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.
It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.
Inner classes make sense when they are tiny and don't need names. Listeners in GUIs are classic examples where they make sense.
If the class is big and important, it should be named and placed in a separate file.
The listener classes in normal GUI examples do one tiny thing, usually just dispatch to some other function to do real work.
I also often use static nested classes (which are technically not inner classes) for classes which are only used in the context of another class - Map.Entry is a good example of this. It's only used in conjunction with a Map, so having the definition of Entry be a part of the Map interface makes organizational sense.
I don't generally have much use for other types of nested classes, like nonstatic member classes and local classes. But they do occasionally come in useful. For a good example of a legitimate use for member classes, see the source code for LinkedList.ListItr. This is a private inner class whose purpose is to provide an implementation of ListIterator for a LinkedList. To do this, it's useful to have access to the private data inside the LinkedList. To achieve this using only top-level classes, it would have been necessary to expose more public methods in LinkedList to allow the ListIterator to get at the underlying implementation of the LinkedList. Instead, using an inner class allows LinkedList to keep its implementation private, as it should be.
When to use inner classes?
If you think that FooHelper
will not at all be useful for other classes than Foo
, then it makes sense to make it as private
inner class of Foo
. One example of this kind of design can be found in HashMap
where it defines a private inner class KeySet
Otherwise having it as a private
instance looks good.