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 |
|---|---|---|
| Returns name | "MIDTERM" |
| Position (starts at 0) | MIDTERM = 1 |
| Checks equality | false if different |
| Compares order | positive/negative/0 |
π§ Example Breakdown:
p = Period.MIDTERM;p.toString()β"MIDTERM"p.ordinal()β1p.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 |
|---|---|
| Converts String β Enum |
| Returns all enum values |
π Example:
Period.valueOf("PRELIM"); // returns PRELIMfor(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 RealEstateListingInner 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 enumvalues()β 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)