Skip to content

Inner Classes, Anonymous & Enum 🏒

Mentor's Note: Not every class needs to be a "Global Citizen." Some classes only exist to help another class (Inner), some are used just once and thrown away (Anonymous), and some are just a list of fixed choices (Enum). πŸ’‘


🌟 The Scenario: The Smartphone System πŸ“±

Think about a high-end smartphone.

  • The Inner Class (The Battery): The Battery lives inside the phone. It isn't sold as a standalone device; it's part of the phone and uses its internal power system. πŸ”‹
  • The Anonymous Class (The One-Time Update): An OTA Update runs once to fix a bug and is then deleted. You don't create a permanent class for it; you just "run and done." 🎟️
  • The Enum (The Model Color): You can only buy the phone in BLACK, WHITE, or GOLD. These are fixed choices that never change. 🎨

🎨 Visual Logic: The Smartphone Interior

graph TD
    subgraph Outer: ["Smartphone πŸ“±"]
    A[Battery πŸ”‹ (Inner Class)]
    B[Camera πŸ“· (Inner Class)]
    end
    C[OS: ANDROID βš™οΈ (Enum)]
    D[One-time Fix 🎟️ (Anonymous Class)]

πŸ“– Concept Explanation

1. Inner Classes (Nested)

An Inner Class is a class defined inside another class. It has a "Special Power": It can access private members of the Outer class! - Use: Group classes that belong together logically.

2. Anonymous Classes πŸ‘€

An Anonymous class is a class without a name. You declare it and instantiate it at the same time. - Use: When you only need to override a method for one-time use (like an event listener).

3. Java Enums (Fixed Choices) 🏷️

An enum is a special "class" that represents a group of Constants (unchangeable variables). - Use: When you have a predefined set of values like Days, Months, or Colors.


πŸ’» Implementation: The Advanced Lab

class Smartphone {
    private int power = 100;

    class Battery { // πŸ”‹ Inner Class
        void showPower() {
            // Can access private 'power' from Smartphone!
            System.out.println("Remaining Power: " + power + "%");
        }
    }
}
abstract class Update {
    abstract void applyFix();
}

public class Main {
    public static void main(String[] args) {
        Update myFix = new Update() { // πŸ‘€ Anonymous Class
            void applyFix() {
                System.out.println("Patching security bug... Done! πŸ›‘οΈ");
            }
        };
        myFix.applyFix();
    }
}
enum ModelColor {
    BLACK, WHITE, GOLD // 🎨 Constants
}

public class Main {
    public static void main(String[] args) {
        ModelColor myColor = ModelColor.GOLD;
        System.out.println("Selected Color: " + myColor);
    }
}

πŸ“Š Sample Dry Run (Enum Switch)

Step Action Logic Result
1 ModelColor.GOLD Lookup the constant list Constant found βœ…
2 switch(myColor) Compare with case "GOLD" Match found! 🎯

πŸ“ˆ Technical Analysis

  • Inner Class Lifetime: An inner class object can only exist if an outer class object exists. You first create the Phone, then you get the Battery! πŸ—οΈ
  • Anonymous Class limitation: You cannot write a constructor for an anonymous class because it doesn't have a name to use!

🎯 Practice Lab πŸ§ͺ

Task: The Traffic Light

Task: Create an enum called TrafficLight with RED, YELLOW, and GREEN. Use a switch statement to print "Stop," "Wait," or "Go" based on the light. Goal: Master using Enums for decision-making. πŸ’‘


πŸ’‘ Interview Tip πŸ‘”

"Interviewers love asking: 'What is the difference between a static inner class and a regular inner class?' Answer: A regular inner class needs an object of the outer class to exist. A static inner class does not! It’s like a separate class that just happens to be inside another one."


πŸ’‘ Pro Tip: "Enums make your code type-safe. Instead of checking if a string is 'RED', you check the Enum. No more typos!" - Vishnu Damwala


← Back: Packages & API | Next: Exception Handling β†’