Benefit of using Nested class in java
Asked Answered
M

6

5

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?

Mesentery answered 8/1, 2013 at 16:4 Comment(5)
do you mean inner class?Lendlease
just check the doc docs.oracle.com/javase/tutorial/java/javaOO/nested.htmlDawndawna
yes, it is inner class but in Java Terminology some call nestedclass! docs.oracle.com/javase/tutorial/java/javaOO/nested.htmlMesentery
enum sub-classes, method local and anonymous classes are nested classes. Do you specific mean named nested classes?Veolaver
they are very high level description any realistic examples?Mesentery
K
9

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
}
Klaxon answered 8/1, 2013 at 16:8 Comment(2)
I 've read that before. But need an example that shows the power of this concept. In many cases the composite pattern is the best one. It separates the complexity of different classes (parents/child for example).Mesentery
@Mesentery I've added a small example usage.Klaxon
A
4

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. :)

Austriahungary answered 8/1, 2013 at 16:8 Comment(0)
V
2

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.

Veolaver answered 8/1, 2013 at 16:10 Comment(0)
C
2

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 :)

Corson answered 8/1, 2013 at 16:12 Comment(0)
D
0

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.

Degraded answered 8/1, 2013 at 16:14 Comment(0)
P
0

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.

Plasmosome answered 8/1, 2013 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.