Skip to content

Commit 4dd7d05

Browse files
committed
feat: demonstrate enum declaration inside and outside a class in Java
WHAT was added: - Created a global enum `Color { RED, GREEN, BLUE }` and used it in `Enum3`. - Demonstrated declaring an enum *inside* a class (`Enum4`) and using it similarly. - Printed enum constants to show usage. WHY this matters: - Enums in Java can be declared at **top-level** (outside classes) or as **nested types** (inside a class). - Helps developers understand valid scopes for enums: - Outside any class (global scope). - Inside a class (nested enum). - Not allowed inside a method. HOW it works: - `Enum3` uses a globally declared enum `Color`. - `Enum4` declares its own nested `Color` enum and references it within `main()`. - Both approaches allow constants to be accessed as `Color.RED`. REAL-LIFE APPLICATIONS: - Top-level enums → shared across packages, e.g., `Status { PENDING, RUNNING, COMPLETED }`. - Nested enums → tied to a specific class context, e.g.: - `Order.State { NEW, PROCESSING, DELIVERED }` inside `Order` class. - `TrafficLight.Color { RED, YELLOW, GREEN }` inside `TrafficLight` class. NOTES / BEST PRACTICES: 1. Enum constants are implicitly `public static final`. 2. Enums should be named in singular form (`Color` instead of `Colors`). 3. Constants should follow UPPERCASE naming convention (`RED, GREEN, BLUE`). 4. Avoid declaring enums inside methods — this is not supported in Java. 5. Use top-level enums when they are reused widely, and nested enums when they are strongly coupled to a single class. KEYWORDS: enum, nested enum, top-level enum, declaration rules, type-safe constants. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 132e986 commit 4dd7d05

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed
Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,50 @@
1-
/*
2-
Enum declaration can be done outside a class or inside a class but not inside a method.
3-
A simple enum example where enum is declared
4-
outside any class (Note enum keyword instead of
5-
class keyword)
6-
*/
71
enum Color {
82
RED,
93
GREEN,
104
BLUE;
115
}
6+
127
public class Enum3 {
13-
// Driver method
148
public static void main(String[] args) {
159
Color c1 = Color.RED;
1610
System.out.println(c1);
1711
}
1812
}
1913

2014
// enum declaration inside a class.
21-
class Enum4 extends Enum3
22-
{
23-
enum Color
24-
{
15+
class Enum4 extends Enum3 {
16+
enum Color {
2517
RED,
2618
GREEN,
2719
BLUE;
2820
}
29-
// Driver method
3021
public static void main(String[] args) {
3122
Color c1 = Color.RED;
3223
System.out.println(c1);
3324
}
34-
}
25+
}
26+
27+
/*
28+
Enum declaration rules:
29+
1. Enum ko class ke bahar ya class ke andar declare kar sakte ho.
30+
2. Lekin enum ko method ke andar declare nahi kar sakte.
31+
3. enum keyword use karte ho (class keyword ki jagah).
32+
33+
1. Enum bahar bhi declare kar sakte ho (global scope)
34+
→ "enum Color { ... }" outside any class.
35+
36+
2. Enum class ke andar bhi declare kar sakte ho
37+
→ "class Enum4 { enum Color { ... } }"
38+
39+
3. Enum method ke andar declare.(not allowed).
40+
41+
4. Enum constants by default public, static, final hote hain.
42+
Isliye direct access: Color.RED
43+
44+
5. Best practice:
45+
- Enum names → singular (Color not Colors).
46+
- Constants → UPPERCASE (RED, GREEN, BLUE).
47+
48+
Enum declaration can be done outside a class or inside a class but not inside a method.
49+
A simple enum example where enum is declared outside any class (Note enum keyword instead of class keyword).
50+
*/

0 commit comments

Comments
 (0)