Inner Classes (Nested Classes) in JavaLast Updated : 14 Jan 2026 Java inner classes are classes defined within another class that are used to improve encapsulation and code organization. In this chapter, we will learn creating and using inner classes with the help of examples. What is Inner Class (Nested Classes) in Java?An inner class (or nested class) is a class declared inside another class or interface. Inner classes help logically group related classes in one place to make the code more readable and maintainable. They can also access all members of the outer class, including private fields and methods. Creating Inner ClassAn inner class is created by defining a class inside another class. Here are the steps to create an inner class:
SyntaxHere is the syntax to create an inner class: ExampleIn this example, we have an outer class Student with an inner class Address. The inner class stores the student's address and displays it along with the student's name. Output: Student Name: Rahul City: Delhi State: Delhi Advantages of Using Inner ClassesThe following are the main two advantages of using inner classes:
Types of Nested ClassesNested classes are classified into two main types: non-static nested classes and static nested classes. The non-static nested classes are commonly referred to as inner classes. 1. Non-Static Nested Class (Inner Class)Inner classes are non-static classes defined within another class. They are further categorized into:
2. Static Nested ClassA static nested class is a static class defined within another class. It can access only static members of the outer class directly. Nested InterfaceAn interface defined inside a class or another interface. Nested interfaces are often used to logically group related interfaces. Next TopicMember Inner class |
We request you to subscribe our newsletter for upcoming updates.