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.
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
}