Skip to content

Commit 132e986

Browse files
committed
feat: demonstrate enum usage with ordinal(), equality checks, and switch-case
WHAT was added: - Defined an `enum Status { Running, failed, Pending, Success }` to represent task states. - Showcased: - Printing enum constants directly. - Using `ordinal()` to get the constant’s position. - Comparing enum values with `==` for conditional logic. - Using `switch` on enum constants. WHY this matters: - Enums provide type-safe constants, making code more readable and less error-prone than using raw integers or strings. - `ordinal()` shows the position of the enum in its declaration (e.g., `Running → 0`, `Success → 3`). - Equality (`==`) and `switch-case` improve clarity when checking state transitions. HOW it works: 1. Print constants: - `Status.Running → Running` - `Status.Success → Success` 2. Print ordinals: - `Status.Running.ordinal() → 0` - `Status.Success.ordinal() → 3` 3. Conditional check with `if-else` to print messages depending on status. 4. `switch(s)` statement executes matching case block (e.g., `Running → "All Good"`). REAL-LIFE APPLICATIONS: - Workflow states → `PENDING`, `RUNNING`, `FAILED`, `SUCCESS`. - Order status → `PLACED`, `SHIPPED`, `DELIVERED`, `CANCELLED`. - Payment lifecycle → `INITIATED`, `PROCESSING`, `SUCCESS`, `FAILED`. - Job scheduling → `WAITING`, `RUNNING`, `COMPLETED`. NOTES / BEST PRACTICES: - Avoid relying on `ordinal()` for persistence since reordering constants changes values. - Prefer `name()` or custom fields for saving state in databases. - Enum in `switch` improves readability compared to multiple `if-else` chains. KEYWORDS: enum, ordinal, switch-case, workflow states, type-safe constants, real-world enum usage. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 9866223 commit 132e986

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

Section20JAVA.lang.Package/src/Enum2.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
enum Status
2-
{
1+
enum Status {
32
Running, failed, Pending, Success;
43
}
54

65
public class Enum2 {
76
public static void main(String[] args) {
87
int i =5;
8+
99
Status s = Status.Running;
1010
System.out.println(s);
1111
System.out.println(s.ordinal());
@@ -26,8 +26,7 @@ else if(s2==Status.Pending)
2626
System.out.println("Done");
2727

2828
Status s3=Status.Success;
29-
switch(s)
30-
{
29+
switch(s) {
3130
case Running:
3231
System.out.println("All Good");
3332
break;
@@ -45,4 +44,22 @@ else if(s2==Status.Pending)
4544
break;
4645
}
4746
}
48-
}
47+
}
48+
49+
/*
50+
1. ordinal():
51+
- Enum constant ki position (0-based index) return karta hai.
52+
- Example: Running -> 0, failed -> 1, Pending -> 2, Success -> 3.
53+
54+
2. Comparison:
55+
- Enums ko == se compare karte hain (safe hai, equals() ki zaroorat nahi).
56+
- if(s2 == Status.Pending) ✅
57+
58+
3. switch-case:
59+
- Enums directly switch-case me use hote hain.
60+
- Cleaner aur faster than multiple if-else.
61+
62+
4. Enum constants:
63+
- Always UPPERCASE likhne ka convention hota hai (Running → RUNNING).
64+
- Aapke enum me "failed" lowercase hai, technically valid hai but not standard convention.
65+
*/

0 commit comments

Comments
 (0)