Nested and Inner Classes in Java

Java Nested and Inner Classes

A nested class in Java is simply a class defined within another Java class. Java allows nesting classes either as members or within blocks of code. Both classes and interfaces can be nested. A nested class is considered a part of its enclosing class, where both enclosing and enclosed class can access all members of each other. Differences between nested types depend on whether the nested type is a class or an interface, and whether the enclosing type is a class or an interface.

Java Static Nested Classes vs. Non-static or Inner Classes

Sometimes the two terminologies static nested class, and non-static or inner class create confusion and the difference becomes obscure. First of all a class defined within another class is necessarily a nested class. Nested classes come in two flavours: static nested classes and non-static nested classes. All nested classes except static nested classes are inner classes. In other words non-static nested classes are called inner classes, while static nested classes are called static nested classes.

Java Language Specification mentions that an inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static blocks or member interfaces, else there will be a compile-time error. Inner classes may also not declare static members, unless they are constant variables, else again there will be a compile-time error. Inner classes may inherit static members that are not constant variables even though they may not declare them.

Java Language Specification further mentions that nested classes that are not inner classes may declare static members freely, in accordance with the usual rules of the Java programming language. Member interfaces are implicitly static so they are never considered to be inner classes.

Use of Java's Nested Classes

There are a variety of reasons to use Java's nested classes both static and non-static. First, nested classes and nested interfaces allow types to be structured and scoped into logically related groups. 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. Second and more important, nested classes can be used to connect logically related objects simply and effectively. It increases encapsulation. Event handling becomes so easy with inner classes. Anonymous inner classes seem to be a boon for event handling.

Types of Java's Nested Classes

Java's nested classes can be categorized in following four types, and they are discussed in separate tutorials. Click on any of the following to read more about them.

Conclusively, an inner class is a member of the outer class just like other variables and methods, so the same modifiers applied to other variables can be applied to inner class too. For example an inner class can be declared final, abstract, public, private, protected, and strictfp but static because static turns it into a static nested class not an inner class.

Last Word

In this tutorial we talked of Java's nested classes. Nested classes are of two types - static nested classes and non-static nested classes. Non-static nested classes are also called inner classes. Nested classes are proved useful when logical grouping of classes is required. Hope you have enjoyed reading this tutorial. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!


Get Free Tutorials by Email

About the Author

is the main author for He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.

Today's Tech News

WannaCry ransom notice analysis suggests Chinese linkPosted on Monday May 29, 2017

Researchers say the WannaCry ransom note was poorly translated - possibly using Google Translate.

BA boss 'won't resign' over flight chaosPosted on Monday May 29, 2017

Chief executive Alex Cruz says flight disruption at Heathrow and Gatwick had nothing to do with cost cutting.

Hay Festival 2017: Stephen Fry's warning for the webPosted on Sunday May 28, 2017

Delivering a lecture at the Hay Festival, the actor said society could face "dire consequences".

Courtesy BBC News