Nested and Inner Classes

suggest change


[{“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}]


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.



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

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”.

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.

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents