👩💻 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
🔧 Usage:
🔹 2. Static Nested Class
🔧 Usage:
❗ Cannot access non-static outer class members directly.
🔹 3. Local Inner Class
🔧 Usage:
🔹 4. Anonymous Inner Class
Used to create one-time-use subclasses or interface implementations.
✅ 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