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

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)