Skip to content

Commit 11338f6

Browse files
committed
feat: add Department enum with fields, constructor, and accessor methods
WHAT was implemented: - Defined an enum `Department` with constants: CS, IT, ECE, CIVIL. - Each enum constant stores two properties: - `head` → name of the department head. - `location` → physical location (block name). - Added a private constructor to initialize enum-specific fields. - Added getter methods (`getHeadName()`, `getLocation()`) to safely access properties. - In `EnumDemo1.main()`, demonstrated usage by printing details of `Department.CIVIL`. WHY this is important: - Shows that enums in Java are more powerful than simple constants. - Enums can encapsulate data and behavior, similar to classes. - Makes enums highly useful for modeling domain-specific constants with metadata. KEY CONCEPTS COVERED: 1. **Enum with Fields** - Unlike plain enums, each constant can hold custom data. Example: `CS("John", "Block A")`. 2. **Enum Constructor** - Private by default, ensures no external instantiation. - Automatically called once per constant. 3. **Methods inside Enum** - Getters allow controlled access to internal fields. - Enums can also define custom behavior per constant if needed. 4. **Encapsulation in Enums** - Enums combine fixed constants with associated attributes. - Improves readability and reduces hardcoding in business logic. REAL-WORLD APPLICATIONS: - **University/College Systems** → Departments with head and location details. - **Banking/Finance** → Enum of account types with interest rates and policies. - **E-commerce** → Enum of order statuses with descriptions and workflows. - **Transport** → Enum of stations/routes with location metadata. BEST PRACTICES: - Keep enum fields `private final` (immutable after initialization). - Provide getters instead of exposing fields directly. - Use enums instead of parallel arrays or switch-case chains for related constants. - Enum names → singular (Department), constants → uppercase (CS, IT, CIVIL). KEYWORDS: enum with fields, enum constructor, enum methods, encapsulation, department metadata. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 8bbe791 commit 11338f6

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed
Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
1-
//Solve and watch this video Again.
2-
3-
enum Department
4-
{
5-
CS("John","Block A"),IT("Smith","Block B"),CIVIL("Srinivas","Block C"),ECE("Dave","Block D");
1+
enum Department {
2+
CS("John","Block A"),
3+
IT("Smith","Block B"),
4+
ECE("Dave","Block D"),
5+
CIVIL("Srinivas","Block C");
66

77
String head;
88
String location;
99

10-
private Department(String head,String loc)
11-
{
10+
private Department(String head,String loc) {
1211
this.head=head;
1312
this.location=loc;
1413
}
15-
public String getHeadName()
16-
{
14+
15+
public String getHeadName() {
1716
return head;
1817
}
19-
public String getLocation()
20-
{
18+
19+
public String getLocation() {
2120
return location;
2221
}
2322
}
2423

25-
public class EnumDemo1
26-
{
27-
public static void main(String[] args)
28-
{
29-
Department d=Department.CS;
24+
public class EnumDemo1 {
25+
public static void main(String[] args) {
26+
Department d=Department.CIVIL;
3027

3128
System.out.println(d.getHeadName());
3229
System.out.println(d.getLocation());
3330
}
34-
}
31+
}

0 commit comments

Comments
 (0)