Skip to content

Commit 9866223

Browse files
committed
feat: add Enum example demonstrating toString(), name(), and values()
WHAT was added: - Created an enum `Size` with constants: SMALL, MEDIUM, LARGE, EXTRALARGE. - Overrode `toString()` specifically for `SMALL` to print "Pizza is small". - Demonstrated: - `toString()` for custom string output. - `name()` to fetch the exact declared identifier. - `values().length` to count enum constants. WHY this matters: - Enums in Java are type-safe constants, better than using raw `int` or `String` constants. - Demonstrates the difference between: - `toString()` → customizable, can be overridden per constant. - `name()` → fixed, always returns the declared constant name. - `values()` is a handy utility to iterate over all enum constants or count them. HOW it works: 1. `Size.SMALL.toString()` prints "Pizza is small" (overridden). 2. `Size.MEDIUM.name()` prints "MEDIUM" (exact enum identifier). 3. `Size.values().length` prints `4` (total number of constants). REAL-LIFE APPLICATIONS: - Pizza ordering system → `Size.SMALL`, `Size.MEDIUM`, etc. - Traffic lights → `enum Signal { RED, YELLOW, GREEN }`. - Payment status → `enum Status { PENDING, SUCCESS, FAILED }`. - Logging levels → `enum LogLevel { INFO, WARN, ERROR }`. - Role-based access control → `enum Role { ADMIN, USER, GUEST }`. COMPARISON: - `toString()` → can be overridden for user-friendly messages (UI display). - `name()` → always returns constant’s code representation (useful for debugging, persistence). - `ordinal()` (not shown here) → gives numeric index (but not recommended for persistence as ordering may change). KEYWORDS: enum, toString override, name method, values method, type-safe constants, real-world enum use cases. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 7c50554 commit 9866223

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed
Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
11
enum Size
22
{
3-
SMALL{
4-
public String toString()
5-
{
6-
return "Pizza is small";
7-
}
8-
}, MEDIUM, LARGE, EXTRALARGE
3+
SMALL
4+
{
5+
public String toString()
6+
{
7+
return "Pizza is small";
8+
}
9+
},
10+
MEDIUM, LARGE, EXTRALARGE
911
}
12+
1013
public class Enum1 {
11-
public static void main(String[] args)
12-
{
14+
public static void main(String[] args) {
1315
System.out.println(/*"string value of SMALL is " +*/ Size.SMALL.toString());
1416
System.out.println("string value of MEDIUM is " + Size.MEDIUM.name());
1517
System.out.println(Size.values().length);
1618
}
1719
}
1820

19-
/* In Java, we can get the string representation of enum constants using the toString() method or the name() method.*/
21+
/*
22+
1. toString():
23+
- By default -> enum constant ka name return karta hai (jaise name()).
24+
- Lekin override kar sakte ho per-constant basis.
25+
- Example: SMALL -> "Pizza is small" (custom string).
26+
27+
2. name():
28+
- Hamesha constant ka original declared name return karega (SMALL, MEDIUM, ...).
29+
- Isko override nahi kar sakte.
30+
31+
3. values():
32+
- Enum ke sare constants ek array me return karta hai.
33+
- Useful for loops or counting constants.
34+
35+
4. Enum is implicitly final and extends java.lang.Enum.
36+
37+
In Java, we can get the string representation of enum constants using the toString() method or the name() method.
38+
*/

0 commit comments

Comments
 (0)