Nested and Inner Classes
suggest changeVersions
[{“Name”:“Java SE 1.1”,“GroupName”:null},{“Name”:“Java SE 1.2”,“GroupName”:null},{“Name”:“Java SE 1.3”,“GroupName”:null},{“Name”:“Java SE 1.4”,“GroupName”:null},{“Name”:“Java SE 5”,“GroupName”:null},{“Name”:“Java SE 6”,“GroupName”:null},{“Name”:“Java SE 7”,“GroupName”:null},{“Name”:“Java SE 8”,“GroupName”:null},{“Name”:“Java SE 9 (Early Access)”,“GroupName”:null}]
Introduction
Using Java, developers have the ability to define a class within another class. Such a class is called a Nested Class. Nested Classes are called Inner Classes if they were declared as non-static, if not, they are simply called Static Nested Classes. This page is to document and provide details with examples on how to use Java Nested and Inner Classes.
Syntax
- public class OuterClass { public class InnerClass { } } // Inner classes can also be private
- public class OuterClass { public static class StaticNestedClass { } } // Static nested classes can also be private
- public void method() { private class LocalClass { } } // Local classes are always private
- SomeClass anonymousClassInstance = new SomeClass() { }; // Anonymous inner classes cannot be named, hence access is moot. If ‘SomeClass()’ is abstract, the body must implement all abstract methods.
- SomeInterface anonymousClassInstance = new SomeInterface() { }; // The body must implement all interface methods.
Remarks
Terminology and classification
The Java Language Specification (JLS) classifies the different kinds of Java class as follows:
A top level class is a class that is not a nested class.
A nested class is any class whose declaration occurs within the body of another class or interface.
An inner class is a nested class that is not explicitly or implicitly declared static.
An inner class may be a non-static member class, a local class, or an anonymous class. A member class of an interface is implicitly static so is never considered to be an inner class.
In practice programmers refer to a top level class that contains an inner class as the “outer class”. Also, there is a tendency to use “nested class” to refer to only to (explicitly or implicitly) static nested classes.
Note that there is a close relationship between anonymous inner classes and the lambdas, but lambdas are classes.
Semantic differences
- Top level classes are the “base case”. They are visible to other parts of a program subject to normal visibility rules based on access modifier semantics. If non-abstract, they can be instantiated by any code that where the relevant constructors are visible based on the access modifiers.
- Static nested classes follow the same access and instantiation rules as top level classes, with two exceptions:
- A nested class may be declared as
private
, which makes it inaccessible outside of its enclosing top level class. - A nested class has access to the
private
members of the enclosing top-level class and all of its tested class.
This makes static nested classes useful when you need to represent multiple “entity types” within a tight abstraction boundary; e.g. when the nested classes are used to hide “implementation details”.
- Inner classes add the ability to access non-static variables declared in enclosing scopes:
- A non-static member class can refer to instance variables.
- A local class (declared within a method) can also refer to the local variables of the method, provided that they are
final
. (For Java 8 and later, they can be effectively final.) - An anonymous inner class can be declared within either a class or a method, and can access variables according to the same rules.
The fact that an inner class instance can refer to variables in a enclosing class instance has implications for instantiation. Specifically, an enclosing instance must be provided, either implicitly or explicitly, when an instance of an inner class is created.