Java.Core.What is a “local class”? What are its features?

What is a Local Class in Java?

A local class is a type of nested class that is declared inside a method, constructor, or block.

  • It is only accessible within the method where it is defined.
  • It is used for encapsulation and organization when a class is needed only in a specific method.
  • It behaves like an inner class, but with additional scoping restrictions.

Features of Local Classes

1. Defined Inside a Method or Block

  • A local class is declared inside a method (or inside a loop or block).
  • It is not visible outside the method.
class Outer {
    void show() {
        class Local { // Local class inside method
            void display() {
                System.out.println("Inside Local Class");
            }
        }
        Local obj = new Local();
        obj.display();
    }
}

public class Main {
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.show(); // Works fine
    }
}

Output:

Inside Local Class

Local class is not accessible outside show().


2. Cannot Have Static Members

  • Local classes cannot declare static fields, methods, or nested classes.
  • However, they can declare static final constants.

Invalid Example:

void method() {
    class Local {
        static void staticMethod() { // ❌ Error: Local classes cannot have static methods
            System.out.println("Static method in local class");
        }
    }
}

Valid Example (Static Final Constant):

void method() {
    class Local {
        static final int CONSTANT = 10; // ✅ Allowed
    }
}

3. Can Access Final or Effectively Final Variables

  • Local classes can access local variables of the enclosing method, but only if they are final or effectively final (unchanged after initialization).
  • This is because Java captures the value of the variable, not its reference.

Example:

class Outer {
    void method() {
        int num = 10; // Effectively final

        class Local {
            void display() {
                System.out.println("Value: " + num); // ✅ Allowed
            }
        }

        Local obj = new Local();
        obj.display();
    }
}

public class Main {
    public static void main(String[] args) {
        new Outer().method();
    }
}

Output:

Value: 10

If num is modified after being used inside Local, it causes a compilation error.

int num = 10;

class Local {
    void display() {
        System.out.println("Value: " + num);
    }
}

// num = 20; // ❌ Compilation Error: Variable must be final or effectively final

4. Can Extend a Class or Implement an Interface

Local classes can extend a class or implement an interface, just like normal classes.

class Outer {
    void method() {
        class Local extends Thread {
            @Override
            public void run() {
                System.out.println("Local class running a thread...");
            }
        }

        Local thread = new Local();
        thread.start();
    }
}

public class Main {
    public static void main(String[] args) {
        new Outer().method();
    }
}

Output:

Local class running a thread...

5. Cannot Be Used Outside the Method

  • A local class is only visible inside the method where it is declared.
  • Trying to use it outside the method results in a compilation error.
class Outer {
    void method() {
        class Local {
            void show() {
                System.out.println("Local class");
            }
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Local obj = new Local(); // ❌ Compilation error: Local class is not visible here
    }
}

Summary of Local Class Features

FeatureLocal Class Behavior
Declared InsideA method, constructor, or block
VisibilityOnly accessible inside the method
Can Have Static Members?❌ No (except static final constants)
Can Access Outer Class Members?✅ Yes, including private fields
Can Access Method Variables?✅ Yes, but only final or effectively final
Can Extend a Class?✅ Yes
Can Implement an Interface?✅ Yes

When to Use a Local Class?

Encapsulation: If the class is only needed within one method, it keeps the scope limited.
Event Handling: Often used in GUI applications (like in Swing for button clicks).
Threading: Used when a temporary Runnable or Thread class is needed.

This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.