👩💻 Inner Classes in Java

In Java, inner classes are classes defined inside another class. They're used to logically group classes, encapsulate helper logic, or access enclosing class members.


🧱 Types of Inner Classes

Type Defined Where Static? Access to Outer Class Instance?
Member Inner Class Inside another class (non-static) ✅ Yes
Static Nested Class Inside another class (static) ❌ No
Local Inner Class Inside a method ✅ Yes (if effectively final)
Anonymous Inner Class Inside a method/block, no name ✅ Yes

🔹 1. Member Inner Class

class Outer {
    private String msg = "Hello";

    class Inner {
        void show() {
            System.out.println("Message: " + msg);
        }
    }

    void callInner() {
        Inner in = new Inner();
        in.show();
    }
}

 

🔧 Usage:

new Outer().callInner();

🔹 2. Static Nested Class

class Outer {
    static class StaticInner {
        void display() {
            System.out.println("Inside static nested class");
        }
    }
}

🔧 Usage:

Outer.StaticInner obj = new Outer.StaticInner();
obj.display();

❗ Cannot access non-static outer class members directly.


🔹 3. Local Inner Class

class Outer {
    void method() {
        int num = 10;  // Must be effectively final
        class LocalInner {
            void show() {
                System.out.println("Num is: " + num);
            }
        }
        new LocalInner().show();
    }
}

🔧 Usage:

new Outer().method();


🔹 4. Anonymous Inner Class

Used to create one-time-use subclasses or interface implementations.

interface Greet {
    void sayHello();
}

public class Test {
    public static void main(String[] args) {
        Greet g = new Greet() {
            public void sayHello() {
                System.out.println("Hello from anonymous class");
            }
        };
        g.sayHello();
    }
}

 


✅ Advantages

  • Logical grouping of related classes
  • Improved encapsulation
  • More readable and maintainable code
  • Can access outer class private members

⚠️ When to Use What?

Use Case Inner Class Type
One-time implementation of interface Anonymous Inner Class
Helper class with outer instance access Member Inner Class
Utility class without outer instance Static Nested Class
Scoped class inside a method Local Inner Class

 

🧠 Java Inner Class Quiz

1. Which of the following is a valid type of inner class in Java?

A. Static inner class
B. Local inner class
C. Anonymous inner class
D. All of the above
Answer: D. All of the above


2. Can a non-static inner class access private members of the outer class?

A. No
B. Only if outer class is public
C. Yes
D. Only static members
Answer: C. Yes


3. Which inner class can be instantiated without creating an instance of the outer class?

A. Member inner class
B. Local inner class
C. Static nested class
D. Anonymous class
Answer: C. Static nested class


4. What is the limitation of local inner classes?

A. Cannot access outer class members
B. Cannot be abstract
C. Can only access final or effectively final variables
D. Must be static
Answer: C. Can only access final or effectively final variables


5. Which statement about anonymous inner classes is FALSE?

A. They must implement an interface or extend a class
B. They can have a constructor
C. They can be defined in method bodies
D. They have no name
Answer: B. They can have a constructor (They cannot have constructors because they don’t have names.)


6. How do you create an object of a non-static inner class?

A. new Inner()
B. Outer.Inner obj = new Inner();
C. Outer.Inner obj = new Outer().new Inner();
D. Inner obj = new Outer().Inner();
Answer: C. Outer.Inner obj = new Outer().new Inner();


7. Can static nested classes access non-static members of the outer class?

A. Yes
B. Only if the members are public
C. No
D. Only in abstract classes
Answer: C. No


8. What is the default modifier for an inner class?

A. public
B. protected
C. private
D. package-private
Answer: D. package-private


9. Which of the following is TRUE for anonymous inner classes?

A. They must override at least one method
B. They must be declared abstract
C. They can extend multiple classes
D. They can be static
Answer: A. They must override at least one method


10. What is the main use of anonymous inner classes in GUI programming?

A. Performance optimization
B. Creating static utilities
C. Event handling
D. Logging
Answer: C. Event handling

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.