Nested/Inner Classes in Java

OmerM
3 min readNov 23, 2022

--

Photo by vamsi sannala on Unsplash

It is possible to create more than one class in the same .java file, the only rule is there can be only one top-level public class (the name of this class should match with file name).

Nested Classes are categorized mainly into two groups Static and Non-Static.

  • Static Nested Classes
  • Non-Static Nested Classes (Inner Classes)
    - Local Inner Classes (Local Classes shortly)
    - Anonymous Classes
    - Other Inner Classes

There are some notes before some sample codes.

  • Nested Classes are generally declared private or package level (no identifier).
    - It is declared as public in order to create an instance at the main method.
  • It is not widely used but if it is required, it is possible to create an instance of Inner Class, relevant syntax can be seen in the Application Class sample code below.
    - Creating an instance of Outer Class does not mean creating an instance of Inner Class, too. It should be created explicitly somewhere.
    - Enclosing/Outer Class should be initiated before initiating an Inner Class.
  • Nested Classes can access class/instance/local variables.
    - Static Nested Classes can access class variables.
    - Local Inner and Anonymous Classes can access class/instance/local variables.
    - The rest of the Inner Classes can access class/instance variables.

Let’s see some sample codes.

Class A

public class A {
private String str;
private static int num = 10;

public static class NestedClass {
public void funcNestedClass() {
System.out.println("***funcNestedClass has been called.");
System.out.println(" num(class variable): " + num + ")");
}
}

public class InnerClass {
public void funcInnerClass() {
System.out.println("***funcInnerClass has been called.");
System.out.println(" num(class variable): " + num + ")");
System.out.println(" str(instance variable): " + str + ")");
}
}

A(String str) {
this.str = str;
}

public void functionA() {
System.out.println("***functionA has been called.");
System.out.println(" str(instance variable): " + str + ")");

int number = 50;

System.out.println();

class LocalInnerClass {
public void funcLocalInnerClass() {
System.out.println("***funcLocalInnerClass has been called");
System.out.println(" num(class variable): " + num + ")");
System.out.println(" str (instance variable): " + str);
System.out.println(" number (local variable): " + number);
}
}

LocalInnerClass localIC = new LocalInnerClass();
localIC.funcLocalInnerClass();
}
}

Application Class

public class Application {

public static void main(String[] args) {
A a = new A("Test Text");
a.functionA();

System.out.println();

// Non-Static Nested Class (Inner Class)
A.InnerClass innerClass = a.new InnerClass();
innerClass.funcInnerClass();

System.out.println();

// Static Nested Class
A.NestedClass nestedClass= new A.NestedClass();
nestedClass.funcNestedClass();
// It is not possible to use as below.
//A.NestedClass.funcNestedClass();
}
}

/*
OUTPUT:
-------
***functionA has been called.
str(instance variable): Test Text)
***funcLocalInnerClass has been called
num(class variable): 10)
str (instance variable): Test Text
number (local variable): 50
***funcInnerClass has been called.
num(class variable): 10)
str(instance variable): Test Text)
***funcNestedClass has been called.
num(class variable): 10)
*/

In summary, defining classes inside other classes provides logical grouping classes and more readable/maintainable code by increasing encapsulation.

How this can increase encapsulation?

For instance, let’s think about two classes A and B. If we define B inside A, B will be hidden from the outside world. B can still access private members of A.

Please also check my other articles, here.

Thanks for reading.

--

--

OmerM

Senior Software Engineer, sharing my knowledge and what i learn.