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 |
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)