Nested Class in Java

A class that is declared within another class is called a nested class.

Example

public class Outer {

    class Nested {

    }
}

A nested class is a member of its outer enclosing class. A nested class can be declared as private, public, protected or with default(without keyword) scope.

Types of Nested Class

In Java, there are of two types of nested classes:

  1. Static nested class
  2. Inner/Non-static class

Static Nested class

A static nested class is declared with the static keyword. A static nested class cannot access other members of its outer enclosing class.

Example

public class OuterExample {

    /* Static Nested class */
    static class StaticNestedExample {

        public static long generateCode(int num) {
            return num * num;
        }
    }

    /* Accessing inner/nested class from inside a method */
    public long getCode(int num) {
        return StaticNestedExample.generateCode(num);
    }
}

public class NestedExample {

    public static void main(String[] args) {

        OuterExample outerExample = new OuterExample();
        long code = outerExample.getCode(2);
        System.out.println(code);
    }
}
Output
4

Inner/Non-static Nested class

A nested class which is declared without the static keyword is called inner/non-static nested class.

A inner/non-static class can access other members of its outer enclosing class even if the scope of those members are private.

Example

public class OuterExample {

    /* Nested/Inner class */
    private class InnerExample {

        public long generateCode() {
            return 1;
        }
    }

    /* Accessing inner/nested class from inside a method */
    public long getCode() {
        InnerExample innerExample = new InnerExample();
        return innerExample.generateCode();
    }
}

public class NestedExample {

    public static void main(String[] args) {
        OuterExample outerExample = new OuterExample();
        long code = outerExample.getCode();
        System.out.println(code);
    }
}
Output
1
Example

Here's another example of calling method of inner class in another class:


public class OuterExample {

    /* Nested/Inner class */
    public class InnerExample {

        public long innerMethod() {
            return 5;
        }
    }

    /* Accessing inner/nested class from inside a method */
    public long outerMethod() {
        return 2;
    }
}

public class NestedExample {

    public static void main(String[] args) {

        OuterExample outer = new OuterExample();
        OuterExample.InnerExample inner = outer.new InnerExample();

        System.out.println(inner.innerMethod());
        System.out.println(outer.outerMethod());
    }
}
Output
5
2

The non-static nested or inner class are of two types:

  1. The local type - A nested class when used inside the body of a method is called local nested class.
  2. The anonymous type - A class with no name is called anonymous nested class. We cannot create an instance of an anonymous class. It either extends a class or implement an interface.

When to Use Nested Classes

Nested classes can be used in the following cases:

  • When a class is useful only to one class then it is logical to keep the both classes together by declaring one class within another class.
  • The use of nested class increases the use of encapsulation by restricting direct access of inner class members from the outside world and thus making the members of nested class more secure. However, the enclosing class can access the members of inner classes.