KTU OOP Java2024 Scheme • 60M ESE
MODULE 2/Section 1: Polymorphism

Inner Classes in Java: Types, Access Rules & Syntax

MEDIUM PRIORITY (Prep Guide)Official Syllabus + Supplementary~14 min study
Official KTU 2024 Syllabus Topics Covered Here
Inner classesTypes of inner classes and basic syntax

1. Topic Introduction

Sometimes one helper class only makes logical sense inside another class—for example, a button click listener tied tightly to one window, or an internal iterator belonging to a data structure. Java allows nesting a class inside another class (Nested / Inner Classes), giving it privileged access even to the outer class's private members!

2. Start From Zero: Heart Inside a Body (Inner Class) vs Detached Workshop (Top-Level Class)

A heart doesn't exist floating independently—it lives inside the body (Outer class) and has direct access to the bloodstream (private fields!). Java provides 4 flavors of nested classes: a Member Inner Class (lives attached to an outer object), a Static Nested Class (belongs to the outer class template without needing an outer object), a Local Inner Class (defined temporarily inside a single method), and an Anonymous Inner Class (a nameless one-time subclass/implementation written inline—used heavily in Swing event listeners!).

Key Takeaway: Non-static inner classes can access ALL outer members (including private) and require an outer instance (outer.new Inner()). Static nested classes don't need an outer instance (new Outer.StaticNested()).

3. Essential Exam Definitions

Nested Class / Inner Class
A class declared within the body of another class (or method). Non-static nested classes are termed Inner Classes; static ones are termed Static Nested Classes.
Anonymous Inner Class
A nameless local class declared and instantiated in a single expression (new Interface() { ... };), widely used in Swing/event handling and threads.

4. Detailed Technical Explanation & Syntax

The 4 Types of Nested / Inner Classes in Java

Java defines four distinct nested-class forms—memorize their syntax and instantiation differences:

  • 1. Member (Non-Static) Inner Class: Declared at class level inside Outer without static. Has access to ALL Outer fields (even private). Instantiated via Outer.Inner in = outerObj.new Inner();.
  • 2. Static Nested Class: Declared with static class Nested {}. Does NOT hold an implicit link to an Outer instance—so it accesses ONLY static members of Outer directly. Instantiated via new Outer.Nested();.
  • 3. Local Inner Class: Declared inside a method or if/for block; visible only within that method and can access local variables only if they are final or effectively final.
  • 4. Anonymous Inner Class: Declared & instantiated inline with no class name when overriding a single class or implementing an interface on the spot (e.g., new ActionListener() { ... } in Module 4!).

5. Working Principle: How Inner Classes Compile (Outer$Inner.class)

  1. When compiling Outer.java containing Inner, javac emits separate bytecode files: Outer.class and Outer$Inner.class (or Outer$1.class for anonymous classes).
  2. Non-static inner classes store a synthetic hidden reference (this$0) pointing back to their enclosing Outer object.

7. Complete Java Code Examples & Tracing

All 4 Types of Inner/Nested Classes in One Runnable ProgramInnerClassDemo.java
Demonstrates Member Inner Class, Static Nested Class, Local Inner Class, and Anonymous Inner Class implementing an interface.
1interface Greeting {
2 void greet();
3}
4
5public class InnerClassDemo {
6 private String outerSecret = "KTU-Private-Data";
7 private static String university = "APJ Abdul Kalam Technological University";
8
9 // 1. Member (Non-Static) Inner Class
10 class MemberInner {
11 void reveal() {
12 System.out.println("1. MemberInner reads outer private: " + outerSecret);
13 }
14 }
15
16 // 2. Static Nested Class
17 static class StaticNested {
18 void showUni() {
19 System.out.println("2. StaticNested reads static outer: " + university);
20 }
21 }
22
23 void demonstrateLocalAndAnonymous() {
24 final String room = "LH-204"; // Effectively final local variable
25
26 // 3. Local Inner Class inside a method
27 class LocalHelper {
28 void printRoom() {
29 System.out.println("3. LocalInner accesses outer + final local: " + outerSecret + " @ " + room);
30 }
31 }
32 new LocalHelper().printRoom();
33
34 // 4. Anonymous Inner Class implementing Greeting
35 Greeting g = new Greeting() {
36 @Override
37 public void greet() {
38 System.out.println("4. AnonymousInner says: Good luck in ESE!");
39 }
40 };
41 g.greet();
42 }
43
44 public static void main(String[] args) {
45 InnerClassDemo outer = new InnerClassDemo();
46 InnerClassDemo.MemberInner m = outer.new MemberInner();
47 m.reveal();
48
49 InnerClassDemo.StaticNested sn = new InnerClassDemo.StaticNested();
50 sn.showUni();
51
52 outer.demonstrateLocalAndAnonymous();
53 }
54}
Expected Output
1. MemberInner reads outer private: KTU-Private-Data
2. StaticNested reads static outer: APJ Abdul Kalam Technological University
3. LocalInner accesses outer + final local: KTU-Private-Data @ LH-204
4. AnonymousInner says: Good luck in ESE!
Keywords:classstaticfinalnewinterfaceprivate
Common Exam & Coding Mistakes
  • Trying to instantiate a non-static inner class inside main directly (new MemberInner()) without an outer instance outer.new MemberInner().
  • Trying to access non-static outer fields from inside a static class nested class.
  • Mutating a local variable referenced by a local/anonymous inner class (violates effectively final rule!).

Advantages

  • Logical grouping of helper classes closely tied to one enclosing class.
  • Stronger encapsulation—inner helper can stay hidden while still reading outer private state.
  • Clean concise syntax for one-off GUI event handlers via anonymous inner classes.

Limitations / Disadvantages

  • Overusing deeply nested classes reduces code readability.
  • Non-static inner class instances retain a hidden reference to the outer object, which can delay garbage collection if misused.

Real-World Applications

  • Swing GUI listeners (button.addActionListener(new ActionListener() { ... })) and SwingUtilities.invokeLater(new Runnable() { ... }).
  • Java Collection iterators (ArrayList$Itr) implemented as private member inner classes.

Important KTU Exam Points

  • Memorize all 4 types of inner classes and their exact instantiation syntax (outer.new Inner() vs new Outer.StaticNested()).
  • Highlight that inner classes can access private fields of the outer class.

Memory Techniques & Revision

  • 4 Inner Classes — 'M-S-L-A': Member (needs outer.new), Static (new Outer.Nested), Local (inside method), Anonymous (nameless inline).

Common Pitfalls to Avoid

  • Confusing Static Nested Class instantiation (new Outer.StaticNested()) with Member Inner Class instantiation (outer.new MemberInner()).

KTU-Style Questions & Exam-Ready Answers

Structured specifically for KTU Part A (3-mark) and Part B (6/9-mark) evaluation rubrics.

Introduction: An inner/nested class is a class defined inside another class to logically group tightly coupled helpers and grant access to the outer class's private members.

  • 1. Member Inner Class: Non-static class inside outer class; instantiated as outerObj.new Inner().
  • 2. Static Nested Class: static class Nested inside outer class; accesses only static outer members; instantiated as new Outer.Nested().
  • 3. Local Inner Class: Defined inside a method/block; accesses outer members and final/effectively final locals.
  • 4. Anonymous Inner Class: Nameless class created inline via new InterfaceOrClass() { ... }; (widely used in Swing events).

Quick Understanding Check

1. How do you instantiate a non-static Member Inner Class Engine inside Car from outside Car?