ENUMERATIONS AND NESTED CLASSES

🧠 ENUMERATIONS AND NESTED CLASSES (Mini Study Guide)


πŸ”Ή 1. ENUMERATIONS (ENUM)

πŸ“Œ What is an Enum?

An enumeration (enum) is a data type with a fixed set of constants.
πŸ‘‰ Meaning: limited lang ang possible values β€” hindi pwedeng kung ano-ano.

πŸ’‘ This part is important kasi ito β€˜yung foundation ng topic.

πŸ” Example:
public enum Period {
 PRELIM, MIDTERM, PREFINAL, FINAL;
}
🧠 Real-Life Analogy:

  • Days of the week β†’ Monday to Sunday lang (walang β€œFunday” ❌)


  • Traffic light β†’ RED, YELLOW, GREEN lang


❗ Why Use Enum?

Without Enum 😬

With Enum 😎

Possible invalid values (e.g., "MIDTRM")

Strict values only

Errors appear at runtime

Errors caught at compile time

Less safe

Type-safe

πŸ‘‰ In short: Enums = SAFE + ORGANIZED


πŸ§ͺ Declaring & Using Enum
Period p;
p = Period.MIDTERM;

πŸ‘‰ Think of it like:

β€œp can ONLY be one of the official periods.”


πŸ”Ή 2. ENUM METHODS (Built-in)

Enums are actually classes, so may built-in methods sila 🀯

πŸ“Š Important Methods:

Method

What it does

Example

toString()

Returns name

"MIDTERM"

ordinal()

Position (starts at 0)

MIDTERM = 1

equals()

Checks equality

false if different

compareTo()

Compares order

positive/negative/0


🧠 Example Breakdown:
p = Period.MIDTERM;
  • p.toString() β†’ "MIDTERM"

  • p.ordinal() β†’ 1

  • p.equals(Period.PRELIM) β†’ false

πŸ‘‰ In short: parang may ranking system yung enums.


πŸ”Ή 3. STATIC METHODS IN ENUM

These are called using the enum type itself, not the variable.

πŸ“Œ Methods:

Method

Purpose

valueOf()

Converts String β†’ Enum

values()

Returns all enum values


πŸ” Example:
Period.valueOf("PRELIM"); // returns PRELIM
for(Period p : Period.values()) {
 System.out.print(p);
}

πŸ‘‰ Loop through all values β€” parang list lang.


🧠 Real-Life Use:

User input β†’ convert to enum (like grading system)

β€œMidterm” β†’ becomes MIDTERM


πŸ”Ή 4. ENUM + SWITCH STATEMENT

Enums work perfectly with switch 😎

πŸ• Example:
enum Size {REGULAR, LARGE, PARTY};

switch(s) {
 case REGULAR: ...
 case LARGE: ...
 case PARTY: ...
}
🧠 Real-Life Analogy:

Parang menu selection:


  • REGULAR β†’ β‚±381


  • LARGE β†’ β‚±602


  • PARTY β†’ β‚±799

πŸ‘‰ Clean, readable, no magic strings πŸ‘Œ


⚑ Key Concept: TYPE-SAFE

πŸ‘‰ Meaning:

Only valid values are allowed.

No weird inputs. No nonsense.


πŸ”Ή 5. NESTED CLASSES

πŸ“Œ What is a Nested Class?

A class inside another class.

πŸ‘‰ Parang:

β€œClass within a class” (yes, Java Inception 😡)


🎯 Why Use It?

  • Better organization


  • Related classes are grouped together


  • Easier to maintain

πŸ’‘ In short, ganito lang β€˜yan: ginagamit mo siya kapag tightly related yung classes.


πŸ”Ή 6. TYPES OF NESTED CLASSES

Type

Description

Static Member Class

Access only static members

Non-static member class (Inner Class)

Needs instance, full access

Local Class

Inside a method

Anonymous Class

No name (one-time use)


🧠 Easy Memory Trick:

S.I.L.A.

  • Static

  • Inner

  • Local

  • Anonymous


πŸ”Ή 7. INNER CLASS (IMPORTANT)

πŸ“Œ Example (Real Estate System):

Outer class:

class RealEstateListing

Inner class:

private class HouseData

πŸ‘‰ According to page 4, the inner class stores address and square feet, while the outer class stores listing info


🧠 Why Inner Class?

  • Used ONLY inside the outer class


  • Keeps data private and organized


πŸ” Access Rules:

Feature

Inner Class

Can access outer class private data?

βœ… YES

Can be accessed outside?

❌ (if private)


⚠ Important Note:

  • Inner classes are not commonly used


  • If needed elsewhere β†’ better as separate class


🧠 QUICK SECTION SUMMARY

ENUM:

πŸ‘‰ Fixed values only

πŸ‘‰ Type-safe

πŸ‘‰ Has built-in methods

NESTED CLASSES:

πŸ‘‰ Class inside a class

πŸ‘‰ Improves organization

πŸ‘‰ Used when tightly related


πŸ”₯ FINAL RECAP + MEMORY TRICKS

🧠 ENUM = β€œLIMITED CHOICES ONLY”

Think:

β€œMenu items lang β€” walang freestyle.”


🧠 ENUM METHODS (Mnemonic: TOEC)

  • ToString

  • Ordinal

  • Equals

  • CompareTo


🧠 STATIC ENUM METHODS:

  • valueOf() β†’ string to enum

  • values() β†’ all values

πŸ‘‰ Think: β€œVALUE & VALUES” = convert + list


🧠 NESTED CLASSES:

Think:

β€œHelper class inside main class”


🧠 TYPES (SILA):


  • Static


  • Inner


  • Local


  • Anonymous


πŸ’¬ FINAL WORD (Gen Z Mode)

If you remember nothing else:

πŸ‘‰ Enums = controlled values (safe coding)

πŸ‘‰ Nested classes = organized code (clean structure)