Summary: In this tutorial, you will learn about the Java nested class which is a class defined within another class.
Introduction to the Java nested class
In Java, you can define a class within another class. This type of class is known as a nested class:
class OuterClass{
// ...
class NestedClas{
// ...
}
}
Code language: Java (java)
Java divides the nested classes into two types:
- Non-static nested classes.
- Static nested classes.
A non-static nested class is referred to as an inner class, whereas a static nested class is simply called a static nested class.
Inner class
An inner class is a class nested within another class. Let’s take an example of defining an inner class and creating its object.
First, define an Outer
class:
public class Outer {
private final int outerValue;
public Outer(int outerValue) {
this.outerValue = outerValue;
}
public void show() {
System.out.println(outerValue);
}
}
Code language: Java (java)
The Outer
class includes the following members:
- A final field
outerValue
- A method
show()
that displays theouterValue
- A constructor that accepts an int and assigns it to the
outerValue
.
Second, define an Inner
class within the Outer
class:
public class Outer {
private final int outerValue;
public Outer(int outerValue) {
this.outerValue = outerValue;
}
public void show() {
System.out.println(outerValue);
}
public class Inner {
private final int innerValue;
public Inner(int innerValue) {
this.innerValue = innerValue;
}
public void show() {
System.out.println(innerValue);
}
}
}
Code language: Java (java)
The Outer
class has the following members:
- The final field
innerValue
. - The constructor that accepts an int and assigns it to the
innerValue
. - The method
show()
that displays the value of theinnerValue
field.
Third, create a new instance of the Outer
class and call the show()
method:
Outer outer = new Outer(10);
outer.show();
Code language: Java (java)
Fourth, create a new instance of the Inner
class:
Outer.Inner inner = outer.new Inner(-10);
Code language: Java (java)
In this syntax, the Outer.Inner
is the type of the Inner
class and the inner
is a reference to the Inner
class:
Outer.Inner inner;
Code language: Java (java)
To access a member of the Outer
class like outerValue
or show()
method, you use an object of the Outer
class.
Similarly, you can create a new instance of the Inner
class via an object of its outer class:
outer.new Inner(-10);
Code language: Java (java)
The whole statement creates a new instance of the Inner
class and assigns it to a reference of the inner variable with the type of Outer.Inner
:
Outer.Inner inner = outer.new Inner(-10);
Code language: Java (java)
Since Java can infer the type of the inner via the Inner
constructor, you can use the var
keyword to make your code more concise:
var inner = outer.new Inner(-10);
Code language: Java (java)
Finally, call the show()
method of the Inner
‘s object:
inner.show();
Code language: Java (java)
Here’s the complete program:
public class App {
public static void main(String[] args) {
Outer outer = new Outer(10);
outer.show();
var inner = outer.new Inner(-10);
inner.show();
}
}
Code language: Java (java)
Output:
10
-10
Code language: Java (java)
Static nested class
If you don’t want to create an instance of the Inner
class via an instance of the Outer
class, you can define the Inner
class as a static nested class as follows:
public class Outer {
private final int outerValue;
public Outer(int outerValue) {
this.outerValue = outerValue;
}
public void show() {
System.out.println(outerValue);
}
public static class Inner {
private final int innerValue;
public Inner(int innerValue) {
this.innerValue = innerValue;
}
public void show() {
System.out.println(innerValue);
}
}
}
Code language: Java (java)
To access a static member of a class, you use the class name, followed by a dot, and then the static member. The same syntax is applied to the static nested class.
The following program creates a new instance of the Inner
class through the Outer
class:
var inner = new Outer.Inner(-10);
inner.show();
Code language: Java (java)
Here’s the complete program:
public class App {
public static void main(String[] args) {
Outer outer = new Outer(10);
outer.show();
var inner = new Outer.Inner(-10);
inner.show();
}
}
Code language: Java (java)
Output:
10
-10
Code language: plaintext (plaintext)
When to use Java nested classes
You can use nested classes to improve code organization, encapsulation, and readability. Here are some common scenarios where you might consider using nested classes:
1) Grouping utility classes
You can use static nested classes to group related utility classes within a larger class. This allows you to keep the utility classes closely tied to the outer class and avoid cluttering the package.
For example:
class Math {
static class Calculator {
// ...
}
}
Code language: Java (java)
2) Iterator and Iterable Patterns
When you implement a custom data structure like a list, you can use an inner class to define an iterator. The reason is that the inner class can access the private members of the collection class.
For example:
class MyList<T> {
private T[] elements;
public Iterator<T> iterator() {
return new MyListIterator();
}
private class MyListIterator implements Iterator<T> {
// ...
}
}
Code language: Java (java)
3) Event Handling
In graphical user interfaces (GUI) applications, you can use inner classes for event handling. For example, you can define each event listener as an inner class, allowing it to access the UI widget and other related data.
For example:
class PushButton {
void addClickListener(ClickListener listener) {
// ...
}
interface ClickListener {
void onClick();
}
}
Code language: Java (java)
Summary
- A nested class is a class defined within another class.
- An inner class is a non-static nested class.
- A static nested class is a nested class defined as a static class.
- Use Java nested classes to improve code organization, encapsulation, and readability