What are Inner Classes in Java?

What are Inner Classes in Java?

In Java, an inner class is a class that is defined inside another class. It is also known as a nested class, and it exists to represent a strong relationship between two classes.

Inner classes make the code more organized, readable, and secure. They are mainly used when a class is tightly bound to the outer class and needs to access its private members directly.

For example, if you have a class Car, an inner class like Engine perfectly fits inside it, because the engine doesn’t exist independently outside of a car.

Types of Inner Classes in Java

Java provides four main types of inner classes:

  1. Member Inner Class
  2. Static Nested Class
  3. Local Inner Class
  4. Anonymous Inner Class

Each type serves a specific purpose and has unique features.

1. Member Inner Class

A member inner class is a non-static class declared inside another class. It can access all members of the outer class, even private ones. You first need to create an object of the outer class to use it.

Example of Member Inner Class:

class Library {
private String bookTitle = "The Java Handbook";

// Inner class
class Book {
void showBook() {
System.out.println("Reading: " + bookTitle);
}
}
}

public class Main {
public static void main(String[] args) {
Library library = new Library(); // Outer class object
Library.Book book = library.new Book(); // Inner class object
book.showBook();
}
}

Output:

Reading: The Java Handbook

Explanation:

  • Here, Book is a member inner class inside Library. It directly accesses the private variable bookTitle of the outer class.

2. Static Nested Class

A static nested class is declared using the static keyword inside another class. It does not need an object of the outer class to be created. However, it can only access static members of the outer class directly.

Example of Static Nested Class:

class Company {
static String companyName = "CodeTech Solutions";

static class Department {
void displayInfo() {
System.out.println("Welcome to " + companyName + " Department!");
}
}
}

public class Main {
public static void main(String[] args) {
Company.Department dept = new Company.Department(); // No outer object needed
dept.displayInfo();
}
}

Output:

Welcome to CodeTech Solutions Department!
  • Here, Department is a static nested class inside Company.

3. Local Inner Class

A local inner class is declared inside a method or block. It can access final or effectively final variables from the method in which it is defined.

Example of Local Inner Class:

class Calculator {
void calculateSum() {
int num1 = 8;
int num2 = 12;

// Local Inner Class inside method
class Adder {
void showResult() {
System.out.println("Sum: " + (num1 + num2));
}
}

Adder adder = new Adder();
adder.showResult();
}
}

public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.calculateSum();
}
}

Output:

Sum: 20

4. Anonymous Inner Class

An anonymous inner class does not have a name. It is used to provide an immediate implementation of an interface or to override the methods of a class.

Example of Anonymous Inner Class:

interface Greeting {
void sayHello();
}

public class Main {
public static void main(String[] args) {
// Creating an anonymous inner class implementing Greeting
Greeting greet = new Greeting() {
public void sayHello() {
System.out.println("Hello! Nice to meet you");
}
};

greet.sayHello();
}
}

Output:

Hello! Nice to meet you

Advantages of Using Inner Classes

  1. Encapsulation: Inner classes logically group classes together and improve encapsulation.
  2. Code Readability: Using inner classes makes the code more organized. When related classes are defined together, it becomes easier for developers to understand the structure and flow of the program.
  3. Access to Outer Class Members: One of the biggest benefits of inner classes is that they can directly access private members of the outer class.
  4. Reduced Complexity: When you use inner classes, you don’t need to create separate top-level classes for every small helper function or logic.

When to Use Inner Classes

  • Use when the inner class needs access to all members (including private) of the outer class.
  • Use when the inner class does not depend on the outer class instance and only needs access to static data or methods.
  • Use local inner classes for small tasks where the class is only needed within a single method.
  • Use anonymous inner classes for short-lived implementations, such as event handling.

Also Learn Important Topics of Java

Leave a Comment