Java Inner Classes
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java Inner Classes

Harine

Java Inner Classes

In Java, a nested class (commonly called an inner class) is a class defined inside another class. Nested classes help logically group related functionality, improve encapsulation, and make large programs easier to maintain.

Using inner classes is a common design technique when one class is tightly coupled to another and is not intended for independent use.

What Is an Inner Class in Java?

A class declared inside another class is called an inner (non-static nested) class. It can access all members of the outer class, including private fields and methods.

Basic synta

class OuterClass {

   class InnerClass {

      // inner class members

   }

}

Creating and Accessing an Inner Class

To use a non-static inner class, you must first create an object of the outer class, then create the inner class object from it.

Example: Accessing inner class members

class Library {

   String libraryName = "City Library";

class Section {

   String sectionName = "Technology";

   void display() {

      System.out.println(sectionName + " section in " + libraryName);

   }

}

}

public class InnerClassDemo {

  public static void main(String[] args) {

   Library library = new Library();

   Library.Section section = library.new Section();

   section.display();

   }

}


Output

Technology section in City Library

Private Inner Class

Unlike top-level classes, inner classes can use access modifiers such as private or protected. A private inner class is accessible only within its outer class.

Example: Private inner class access

class Bank {

   private class Vault {

      void open() {

        System.out.println("Vault opened");

      }

   }

   public void accessVault() {

     Vault vault = new Vault(); // allowed inside outer class

     vault.open();

   }

}

public class PrivateInnerDemo {

   public static void main(String[] args) {

     Bank bank = new Bank();

     bank.accessVault();

     // Not allowed:

     // Bank.Vault vault = bank.new Vault();

    }

}

Key point: External classes cannot access a private inner class directly.

Static Nested Class

A nested class declared with the static keyword is called a static nested class. It behaves like a regular top-level class but is namespaced inside the outer class.

  • Does not require an outer class object
  • Cannot access non-static members of the outer class directly

Example: Static nested class

class University {

   static class Department {

      void show() {

        System.out.println("Computer Science Department");

      }

   }

}

public class StaticNestedDemo {

  public static void main(String[] args) {

    University.Department dept = new University.Department();

    dept.show();

  }

}

Output

Computer Science Department

Inner Class Accessing Outer Class Members

One major advantage of inner classes is that they can directly access all members of the outer class — even private ones.

Example: Inner class accessing outer field

class Smartphone {

   private String model = "Pixel";

   class Processor {

      String getModel() {

        return model; // access outer private field

      }

      }

    }

    public class InnerAccessDemo {

      public static void main(String[] args) {

        Smartphone phone = new Smartphone();

        Smartphone.Processor cpu = phone.new Processor();

        System.out.println(cpu.getModel());

    }

}

Output

Pixel

Types of Nested Classes in Java

Java supports four types of nested classes:

  • Inner class (non-static)
  • Static nested class
  • Local class (inside a method)
  • Anonymous class (inline class)

This chapter focuses on the first two, which are most commonly used in application design.

Why Use Inner Classes?

Inner classes improve code organization and encapsulation.

Benefits

  • Group logically related classes
  • Hide helper classes from external access
  • Improve readability and maintainability
  • Enable tighter coupling when needed
  • Access outer class members easily

Tags
Our website uses cookies to enhance your experience. Learn More
Accept !