what are the benefits of using nested class in Java? In many examples it seems to me it only adds complexity to the design. Is there any example that shows the power of using nested class in comparison to for example composite pattern?
Why Use Nested Classes?
There are several compelling reasons for using nested classes, among them:
- It is a way of logically grouping classes that are only used in one place.
- It increases encapsulation.
- Nested classes can lead to more readable and maintainable code.
(from the docs)
I think that the best case I can think of, off the top of my head would be the implementation of nodes in some sort of collection class (tree, linked list, map, etc). There is no reason why the node implementation should be exposed to the public, and since it is only used by the internals of your collection, it makes sense to make the node class nested inside the collection class.
Something along the lines of..
public class MyTree {
private class TreeNode {
//implementation details...
}
//public api, where implementation references TreeNode
}
I would use a nested class in a case, where I mostly need this class inside the surrounding class. This could be usages like event listeners.
Or in the case when I need to fulfill a specific interface as a parameter to an API method and only create this structure in this specific top level class.
This avoids the creation of more top level classes - messing your code base - that are not useful to any other class.
Hope this helps. :)
Is there any example that shows the power of using nested class in comparison to for example composite pattern?
Nearly all the Iterator implementations and the implementations for keySet() and values() are nested. The actual name is not important but they need access to the outer class to do their work.
Having these be top level classes wouldn't be elegant or encapsulated.
There are many examples I can think of to use nested classes
1- Extending a class for the purpose of using it just inside this instance with a different behavior than usual (overriding a method or more)
2- A short hand for implementing and passing an event listener to whatever method in one single step
3- Implementing a class that is too specific to be defined globally (or with package visibility or private or whatsoever). and you just want to hide it into the larger class
I do agree though that the usage of inner classes would complicate the code no matter how hard you try to make the for formatter beutiful but it has its uses, so as my own rule of thumb I only use inner classes when I'm sure that one of the above applies and that its size will be minimal in order not to vandalize my code
and for that mattar keep all the code minimal :)
We use nested (inner) classes when we know that this class will primarily be used only by the class that it will be written into. It keeps the package structure cleaner. Complexity is reduced by making use of an IDE where you can easily find classes that are not listed in the package structure.
As requested a real world example, take a look at the ArrayList
implementation for the jdk.
You could find it here:
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/ArrayList.java#ArrayList
You will find (among other things) the iterator class inside the Arraylist class. I would guess, the reason for this behavior (as for most of the iterable collection subclasses) is the direct connection between the collection and its iterator. You do not need it outside the class.
You can find nested class in more places inside the jdk.
© 2022 - 2024 — McMap. All rights reserved.