views:

120

answers:

4

In java what are nested classes and what do they do?

+6  A: 

They're just classes within other classes. They make it possible to have a hierarchy of classes, and if you make them private they're a convenient way to encapsulate data that isn't exposed outside of the class using them. Sun has a short tutorial about them

Michael Mrozek
why is this advantagious?
David
As an example I've come accross a neat sorting algorithm that uses an inner class to associate a numeric weight with an item. You will see lot's of examples of these inner classes if you get involved in Swing.
James P.
@David - read the tutorial. The explanation is there.
luis.espinal
+1  A: 

Nested classes are classes that are declared within classes, but have the modifier static. They are a way of organizing the class so that it has access to the private declarations of its outer class, and can be used to better indicate the relationship between the outer and nested class.

They are different from inner classes (declared the same way but without the static) in that an inner class has to have an instance of the outer class as a reference to be able to be instantiated. A nested class, by contrast, isn't tied to a specific instance of the outer class.

EDIT: In response to those who say that a "nested class" can be both a static nested class or an inner class. OK, but you would never call an inner class a nested class, would you? Sure that isn't by the spec (A nested class is any class whose declaration occurs within the body of another class or interface.) But it is how it is used in practice.

Yishai
A nested class does not necessarily have to be `static`.
Mike Daniels
http://java.sun.com/docs/books/tutorial/java/javaOO/nested.htmlTerminology: Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes.
James P.
@Mike, if it is not static, then it is called an inner class.
Yishai
+1 for pointing out the distinction!
Vivin Paliath
Practice of who? Of people misusing the right terminology? My practice is to use the right terminology (i.e. the one of the spec and illustrated in the Sun tutorial).
Pascal Thivent
@Pascal, they might be (I think the spec is unnecessarily convoluted so the language doesn't stick with people), but google reveals it is common. http://www.javaworld.com/javaworld/javatips/jw-javatip75.html
Yishai
@Yishai - No, I think Mike is right. Lets be clear about Sun's terminology. "Nested classes" is *any* class defined within a class. "Static nested class" is a "nested class" with the static modifier. "Inner class" is a "nested class" without the static modifier. "Nested classes" = ("static nested classes" + "inner classes").
Bert F
+2  A: 

One of the most important uses of inner classes in Java are listeners. Instead of writing an entire separate class for an ActionListener or similar, you create it in-place:

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        ... your code here ...
    }
}

These inner classes typically call functions in the outer class (they capture a pointer to the object they were created in), and lead to much tidier code than having a single callback with some logic that has to figure out which button etc. was clicked. You also don't have to modify code in several places when you add or remove a button, since the inner class is created where you create the button. It's quite elegant.

Robert Kosara
A: 

A Nested Class is a class defined within another class. Sun docs indicate you use them because:

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

There are 2 types: static nested classes and inner classes.

A static nested class is a nested class that has been declared static. It is syntactic sugar for what would have been a tightly-coupled top-level class to allow it to be "stashed" within its owning class to indicate its strong relationship to the other class. However, static nested classes don't have any special behaviors or privileges at run-time - its a packaging convenience to achieve the benefits listed above.

An inner class is a nested class that has not been declared static. Unlike a static nested class, inner classes have special rules/behavior that only apply to inner classes. Specifically, an instance of an inner class can only exist in the context of an instance of the outer class, i.e. each inner class instance is tied to a specific outer class instance. An inner class instance has special access to its related outer class instance. This allows for a 'friend'-like access that you might be familiar with in C.

There are plenty of documentation on the subject to learn more, but hopefully that gets you started. Nest logical step is Sun's tutorial on Nested Classes.

Bert F