Inner Classes in Java

Exploring the Types, Advantages, and Practical Use Cases

Nikita Chaurasia
Javarevisited
3 min readApr 23, 2024

--

image : Java Inner Classes

In Java programming, inner classes provide a powerful mechanism for encapsulation and code organization. They allow you to define a class within another class, creating a close relationship between the two. In this guide, we’ll explore the different types of inner classes in Java and their usage with illustrative examples.

Understanding Inner Classes

Inner classes are classes defined within another class, known as the outer class. They create a strong encapsulation and establish a “HAS-A” relationship between the inner class and its outer class. Inner classes are particularly useful for logically grouping classes that are only used in one place, enhancing code readability and maintainability.

Types of Inner Classes

1. Member Inner Class (Nested Inner Class)

A member inner class, also known as a nested inner class or regular class, is a non-static class declared inside an outer class but outside of any method. It can access both static and instance members of its outer class. Let’s take a look at an example:

class Outer {
private int a = 15;

class Inner {
public void displayValue() {
System.out.println("Value of a is " + a);
}
}
}

public class Main {
public static void main(String[] args) {
Outer.Inner inner = new Outer().new Inner();
inner.displayValue();
}
}

2. Method Local Inner Class

Method local inner classes are declared inside a method. They have access to the variables and parameters of the enclosing method but must be marked as final or effectively final. Here's an example:

class MyOuter {
private String x = "Outer class private data";

public void doStuff() {
String x = "local variable";

class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + MyOuter.this.x);
System.out.println("Local variable z is: " + x);
}
}

MyInner mi = new MyInner();
mi.seeOuter();
}
}

public class Test {
public static void main(String[] args) {
MyOuter m = new MyOuter();
m.doStuff();
}
}

3. Static Nested Inner Class

Static nested inner classes are declared with the static keyword inside an outer class. They can only access static members of the outer class. Here's an example:

class Outer {
static int x = 25;

static class Inner {
static void msg() {
System.out.println("x value is " + x);
}
}
}

public class Test {
public static void main(String[] args) {
Outer.Inner.msg();
}
}

4. Anonymous Inner Class

Anonymous inner classes are declared without a name, typically used for extending a class or implementing an interface. They are useful for creating a single-use subclass or interface implementation. Here’s an example:

class Tech {
public void tech() {
System.out.println("Tech");
}
}

public class Test {
public static void main(String... args) {
Tech a = new Tech() {
@Override
public void tech() {
System.out.println("Anonymous tech");
}
};
a.tech();
}
}

Conclusion

Inner classes in Java offer a flexible and powerful way to organize code and create relationships between classes. By understanding the different types of inner classes and their applications, you can leverage them effectively in your Java projects for improved encapsulation and code structure.

Start exploring inner classes in your Java projects today and unlock their full potential!

Thanks for reading

👏 Clap for the story and follow me

📰 Read more content on my Medium

--

--