Skip to main content

Inner Classes, Anonymous & Enum 🏢

Java Inner Classes, Anonymous & Enum is a core Java concept covering master advanced Java concepts: Inner Classes, Anonymous Classes, and Enums. Learn how to nest classes and manage constants using the Smartphone scenario. This topic is essential for academic learning, board exam preparation, and developing optimized real-world code.

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


📖 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 + "%");
}
}
}

📊 Sample Dry Run (Enum Switch)

StepActionLogicResult
1ModelColor.GOLDLookup the constant listConstant found ✅
2switch(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 →

📍 Visit Us

🏫 VD Computer Tuition Surat

VD Computer Tuition
📍 Address
2/66 Faram Street, Rustompura
Surat395002, Gujarat, India
📞 Phone / WhatsApp
+91 84604 41384
🌐 Website

Computer Classes & Tuition — Areas We Serve in Surat

AdajanAlthanAmroliAthwaAthwalinesBhagalBhatarBhestanCanal RoadChowkCitylightDumasGaurav PathGhod Dod RoadHaziraJahangirpuraKamrejKapodraKatargamLimbayatMagdallaMajura GateMota VarachhaNanpuraNew CitylightOlpadPalPandesaraParle PointPiplodPunaRanderRing RoadRustampuraSachinSalabatpuraSarthanaSosyo CircleUdhnaVarachhaVed RoadVesuVIP Road
📞 Call Sir💬 WhatsApp Sir