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!

References




Get Free Tutorials by Email

About the Author

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

Today's Tech News

Robotic barman pours Rory a pintPosted on Friday March 24, 2017

The BBC’s technology correspondent Rory Cellan-Jones gets a beer poured for him by a robotic barman - but how long does it take?

Row over US ISP customer data salesPosted on Friday March 24, 2017

US politicians have voted to let ISPs gather and sell data about what Americans do online.

Ofcom plans instant payback for broadband woesPosted on Friday March 24, 2017

Customers who suffer poor service could get automatic payouts under Ofcom's plan.

Courtesy BBC News