Types of Inner Class
There are two types of Inner class:
1. Local classes
2. Anonymous classes
When working with Java, especially in GUI programming, multithreading, or encapsulating logic inside methods, you'll often come across local classes and anonymous classes.
Let’s break them down with simple explanations and examples.
What is a Local Class?
A local class is a class defined inside a method or a block.
public class Outer {
void greet() {
class Greeter {
void sayHello() {
System.out.println("Hello from local class!");
}
}
Greeter g = new Greeter();
g.sayHello();
}
}
Key Features:
-
Can have a name
-
Can be instantiated multiple times
-
Can access final or effectively final variables from the enclosing method
-
Cannot have static methods or static blocks
-
Can have static final constants
What is an Anonymous Class?
An anonymous class is a one-time-use class with no name, typically used to:
-
Implement an interface, or
-
Extend a class
interface Hey {
void run();
}
public class Main {
public static void main(String[] args) {
Hey task = new Hey() {
@Override
public void run() {
System.out.println("Task is running (anonymous class)");
}
};
task.run();
}
}
Key Features:
-
Has no class name
-
Defined on the spot using
new Interface() { ... }ornew Class() { ... } -
Used when you need an object just once
-
Cannot be reused later — because it’s unnamed
Anonymous Classes Can't Be Reused
This is a common point of confusion:
interface Hey {
void run();
}
public class Main {
public static void main(String[] args) {
Hey task = new Hey() {
@Override
public void run() {
System.out.println("Task is running (anonymous class)");
}
};
task.run();
Hey task2 = new Hey(); // Error: Cannot instantiate the type Hey
}
}
Each anonymous class is defined and used at the same point. Even if two look similar, they're considered different by the compiler.
Can You Use Anonymous Classes Without Interfaces?
Yes, you can also extend a class anonymously:
class Animal {
void speak() {
System.out.println("Animal sound");
}
}
Animal dog = new Animal() {
@Override
void speak() {
System.out.println("Dog barks");
}
};
dog.speak(); // Output: Dog barks
So, anonymous classes can:
-
Implement interfaces
-
Extend abstract or concrete classes
But they must extend or implement something — you can't define one without a base.
Static in Local & Anonymous Classes
In local and anonymous classes:
-
You cannot define
staticmethods orstaticblocks. -
You can define
static finalconstants.
void show() {
class Local {
static final String GREETING = "Hello"; // Allowed
// static void sayHi() {} // Error
}
}
When to Use Which?
| Feature | Local Class | Anonymous Class |
|---|---|---|
| Has a name | Yes | No |
| Reusable | Yes | No |
| Code size | Slightly bigger | Compact |
| Use case | Reuse, readability | One-time quick logic |
| Inherits/Implements | Class or interface | Must extend/implement something |
| Can have multiple methods | Yes | But usually only one method |
Final Tips
-
Use anonymous classes for short, one-time tasks like a quick
Runnableor button click handler. -
Use local classes when the logic is complex and reused within a method.