Java Nested Classes In A NutShell

Umar Ashfaq
Eastros
Published in
5 min readApr 27, 2013

The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here:

[sourcecode language=”java”]
class OuterClass {

class NestedClass {

}

}

[/sourcecode]

You can also refer the offical documentation to see the all the class specs here

Terminology

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.

[sourcecode language=”java”]
class OuterClass {

static class StaticNestedClass {

}

class InnerClass {

}

}

[/sourcecode]

A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private. (Recall that outer classes can only be declared public or package private.)

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.

Logical grouping of classes — If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such “helper classes” makes their package more streamlined.

Increased encapsulation — Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A’s members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.

More readable, maintainable code — Nesting small classes within top-level classes places the code closer to where it is used.

Static Nested Classes

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.

Like any other static method, a static member class has access to all static methods of the parent, or top-level, class.

A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

Static nested classes are accessed using the enclosing class name:

[sourcecode language=”java”]
OuterClass.StaticNestedClass
[/sourcecode]

For example, to create an object for the static nested class, use this syntax:

[sourcecode language=”java”]
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
[/sourcecode]

The benefit of a static nested class over an “ordinary” class is that you can use it to reflect the relationship between two classes.

For example in the JDK there is java.util.Map and java.util.Map.Entry.

java.util.Map.Entry is declared as a public static interface - nested interface, and doing it this way clearly signposts its relationship to Map. It could have been defined as java.util.MapEntry but doing it as a static nested interface makes it clear that it has a strong relationship to Map.

Inner Classes

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object’s methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:

[sourcecode language=”java”]
class OuterClass {

class InnerClass {

}

}

[/sourcecode]

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

[sourcecode language=”java”]
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
[/sourcecode]

Additionally, there are two additional types of inner classes. You can declare an inner class within the body of a method. Such a class is known as a local inner class. You can also declare an inner class within the body of a method without naming it. These classes are known as anonymous inner classes. You will encounter such classes in advanced Java programming.

The anonymous inner classes is very useful in some situation. For example consider a situation where you need to create the instance of an object without creating subclass of a class and also performing additional tasks such as method overloading.

Consider the following code :

[sourcecode language=”java”]
button.addActionListener(new ActionListener() {
public void actionPerfored(ActionEvent e){
// do something.
}
});
[/sourcecode]

As you can see in the above code, i don’t need to create an extra class that implements ActionListener . I initiated anonymous inner class(here it is new ActionListener()) without creating a separate class. You can also perform method overloading by implementing anonymous inner classes.

A comprehensive example that highlights most of the features of nested/inner classes.

[sourcecode language=”java”]

public class OuterClass {
private int pvtMemberVariable = 10;
private static int pvtStaticMemberVariable = 1;

// Non-static Inner Class
// Inner class or non-static nested class
// Can not declare static methods or variables in inner class
public class InnerClass {
// Error: The field someConstant cannot be declared static in a non-static inner
// type, unless initialized with a constant expression
// static int someConstant = 1;
// Lets make it final then
final static int someConstant = 1;

int innerVariable = 20;

private void print() {
// Inner classes have access to other members of the enclosing class,
// even if they are declared private
// Accessing outer class private method
outerPrint();

// Accessing outer class private variable
int sum = innerVariable + pvtMemberVariable;
System.out.println(“[InnerClass] — Accessing outer class private variable — “ + sum);

// Inner class can reference static nested classes as well
StaticNestedClass staticClass = new StaticNestedClass();
staticClass.print();

}

}

// Static nested class
// As a member of the OuterClass, a nested class can be declared private,
// public, protected, or package private
private static class StaticNestedClass{
private int nestedMemberVariable = 10;

public void print(){
System.out.println(“I’m static nested class”);

pvtStaticMemberVariable = 50;

System.out.println(“[StaticNestedClass] — Static Nested Class can “ +
“access outer class static member even if they are private”);
}
}

private void outerPrint() {
System.out.println(“I’m outer class”);
}

public void aMethod() {
int j = 20;

// A class inside a method is treated as local members are treated
// Local class — class inside a method — can only be abstract or final
// It can not be accessed from outside of this method
class LocalClass {
int localClassVariable = 30;

public void print() {
System.out.println(“[LocalClass]”);
System.out.println(“Local class can access members of outer class :”+ pvtMemberVariable );
}
}

LocalClass localClass = new LocalClass();
localClass.print();
}

public static void main(String []args ){
OuterClass outerObject = new OuterClass();

// To instantiate an inner class, first instantiate the outer class
// Then, create the inner object within the outer object
OuterClass.InnerClass innerObject = outerObject.new InnerClass();

// Calling inner object method in outer class
innerObject.print();

outerObject.aMethod();
}
}

[/sourcecode]

Result

[sourcecode language=”java”]

I’m outer class
[InnerClass] — Accessing outer class private variable — 30
I’m static nested class
[StaticNestedClass] — Static Nested Class can access outer class static member even if they are private
[LocalClass]
Local class can access members of outer class :10

[/sourcecode]

Your feedback and suggestions are welcome.

--

--