codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

34 / 65 topics
32Java Inheritance33Java Polymorphism34Java Inner Classes35Java Abstraction36Java Interface37Java Enum
Tutorials/Java Programming/Java Inner Classes
☕Java Programming

Java Inner Classes

Updated 2026-05-12
30 min read

Java Inner Classes

In this tutorial, we will explore the concept of inner classes in Java. Inner classes are a powerful feature that allow you to define one class within another. They can enhance encapsulation, provide better organization, and offer more flexibility in your code structure.

Introduction

Inner classes are a way to logically group classes together and increase the accessibility of your code. They are especially useful when a class is only used by one other class. By nesting classes, you can keep related classes together and hide them from the outside world if necessary.

In this section, we will cover:

  • Nested Classes: Basic inner classes.
  • Private Inner Classes: How to restrict access to inner classes.
  • Static Inner Classes: When an inner class doesn't need to access the outer class's instance members.
  • Accessing Outer Class from Inner Class: How inner classes can interact with their enclosing outer class.

Nested Classes

A nested class is a class defined inside another class. The basic syntax for defining a nested class is as follows:

Java
1public class OuterClass {
2 // Outer class code
3
4 class InnerClass {
5 // Inner class code
6 };
7}

Example: Basic Nested Class

Let's start with a simple example to understand how nested classes work.

NestedExample.java
1public class OuterClass {
2
3 private int outerValue = 10;
4
5 class InnerClass {
6 void display() {
7 System.out.println("Outer value: " + outerValue);
8 }
9 }
10
11 public static void main(String[] args) {
12 OuterClass outer = new OuterClass();
13 OuterClass.InnerClass inner = outer.new InnerClass();
14 inner.display();
15 }
16}
Output
Outer value: 10

In this example, InnerClass is a nested class inside OuterClass. The display method in InnerClass accesses the private variable outerValue of OuterClass.

Key Points

  • Access to Outer Class Members: Nested classes can access all members (including private) of their outer class.
  • Instance Creation: To create an instance of a nested class, you need an instance of the outer class.

Private Inner Classes

You can restrict the visibility of inner classes by using the private keyword. This ensures that the inner class cannot be accessed from outside the outer class.

Example: Private Nested Class

Here's how you can define and use a private nested class.

PrivateNestedExample.java
1public class OuterClass {
2
3 private int outerValue = 20;
4
5 private class InnerClass {
6 void display() {
7 System.out.println("Outer value: " + outerValue);
8 }
9 }
10
11 public void createInnerInstance() {
12 InnerClass inner = new InnerClass();
13 inner.display();
14 }
15
16 public static void main(String[] args) {
17 OuterClass outer = new OuterClass();
18 outer.createInnerInstance(); // This works
19 // InnerClass inner = outer.new InnerClass(); // This will cause a compile-time error
20 }
21}
Output
Outer value: 20

In this example, InnerClass is private and can only be accessed within OuterClass. Attempting to create an instance of InnerClass outside OuterClass results in a compile-time error.

Key Points

  • Restrict Access: Use private to restrict the access of inner classes.
  • Encapsulation: This helps in encapsulating the inner class and preventing it from being used elsewhere.

Static Inner Classes

Sometimes, you might want an inner class that doesn't need to access any instance members of the outer class. In such cases, you can define a static inner class using the static keyword.

Example: Static Nested Class

Here's how you can define and use a static nested class.

StaticNestedExample.java
1public class OuterClass {
2
3 private static int outerValue = 30;
4
5 static class InnerClass {
6 void display() {
7 System.out.println("Outer value: " + outerValue);
8 }
9 }
10
11 public static void main(String[] args) {
12 OuterClass.InnerClass inner = new OuterClass.InnerClass();
13 inner.display();
14 }
15}
Output
Outer value: 30

In this example, InnerClass is a static nested class. It can access only the static members of OuterClass. You don't need an instance of OuterClass to create an instance of InnerClass.

Key Points

  • No Access to Outer Class Instance Members: Static inner classes cannot access non-static (instance) members of the outer class.
  • Independence: They are independent of any specific instance of the outer class.

Accessing Outer Class from Inner Class

An inner class can easily access all members of its enclosing outer class, including private members. This is because an inner class has a reference to its enclosing instance.

Example: Accessing Outer Class Members

Here's how an inner class can access members of its outer class.

AccessOuterExample.java
1public class OuterClass {
2
3 private int outerValue = 40;
4
5 class InnerClass {
6 void display() {
7 System.out.println("Outer value: " + outerValue);
8 }
9 }
10
11 public static void main(String[] args) {
12 OuterClass outer = new OuterClass();
13 OuterClass.InnerClass inner = outer.new InnerClass();
14 inner.display();
15 }
16}
Output
Outer value: 40

In this example, InnerClass accesses the private variable outerValue of OuterClass.

Key Points

  • Implicit Reference: An inner class has an implicit reference to its enclosing instance.
  • Access Control: This allows inner classes to access all members (including private) of their outer class.

Practical Example

Let's create a practical example that combines the concepts discussed so far. We'll create an Account class with nested Transaction and BalanceChecker classes.

AccountExample.java
1public class Account {
2
3 private double balance;
4
5 public Account(double initialBalance) {
6 this.balance = initialBalance;
7 }
8
9 // Nested Transaction class
10 class Transaction {
11 void deposit(double amount) {
12 balance += amount;
13 System.out.println("Deposited: " + amount);
14 }
15
16 void withdraw(double amount) {
17 if (amount <= balance) {
18 balance -= amount;
19 System.out.println("Withdrew: " + amount);
20 } else {
21 System.out.println("Insufficient funds");
22 }
23 }
24 }
25
26 // Static nested BalanceChecker class
27 static class BalanceChecker {
28 void checkBalance(Account account) {
29 System.out.println("Current balance: " + account.balance);
30 }
31 }
32
33 public static void main(String[] args) {
34 Account myAccount = new Account(100.0);
35
36 // Using Transaction inner class
37 Account.Transaction transaction = myAccount.new Transaction();
38 transaction.deposit(50.0);
39 transaction.withdraw(30.0);
40
41 // Using BalanceChecker static nested class
42 Account.BalanceChecker checker = new Account.BalanceChecker();
43 checker.checkBalance(myAccount);
44 }
45}
Output
Deposited: 50.0
Withdrew: 30.0
Current balance: 120.0

In this example:

  • Transaction is a nested class that can access the private balance of Account.
  • BalanceChecker is a static nested class that takes an Account instance to check its balance.

Summary

ConceptDescription
Nested ClassesClasses defined inside another class. Can access all members of the outer class.
Private Inner ClassesRestricts access to inner classes, making them accessible only within the outer class.
Static Inner ClassesIndependent of any specific instance of the outer class; can only access static members.
Accessing Outer ClassInner classes have an implicit reference to their enclosing instance and can access all its members.

What's Next?

In the next section, we will explore Java Abstraction. Abstraction is another fundamental concept in Java that helps you design more flexible and maintainable code. Stay tuned!


PreviousJava PolymorphismNext Java Abstraction

Recommended Gear

Java PolymorphismJava Abstraction