Back to Java Course
Java Course
- Enums are introduced in jdk1.5
- Enums are Fixed no. of, Similiar kind of Constants.
- Because they are constants, the names of an enum type's fields are in uppercase letters.
- Every enum constant is public, static & final by default.
- Every enum constant can be represented as a valid java identifier.
- Every enum constant should be unique.
- All enum constants should be delimeted with comma (,)
- Inside enum, not only enum constats are there,we can keep attributes, methods, constructors, IIB's, SIB's.
- enum can be member of .java file.
- enum can be member of a class.
- For enums also .class files are generated.
- Among all members of one .java file, only one member can be public. Whichever member is public, then that name only can be used for saving the .java file.If enum is public, then that .java file should be saved as enum name.
- enum improves type safety
- enum can be easily used in switch
- enum may implement many interfaces but cannot extend any class because it internally extends Enum class
Example 1
public enum Month { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEPT, OCT, NOV, DEC; public static void main(String[] args) { Month m3 = Month.MAR; System.out.println("m3 : " + m3); Month m5 = Month.MAY; System.out.println("m5 : " + m5); System.out.println("7th month : " + Month.JUL); } }
Output
m3 : MAR m5 : MAY 7th month : JUL
Example 2
public enum JobStatus { PENDING("The job is pending."), IN_PROGRESS("The job is currently in progress."), COMPLETED("The job has been completed."), FAILED("The job has failed."), CANCELLED("The job has been cancelled."); private final String description; // Constructor for the enum JobStatus(String description) { this.description = description; } // Method to get the description of the status public String getDescription() { return description; } @Override public String toString() { return name() + ": " + description; } // Example usage public static void main(String[] args) { for (JobStatus status : JobStatus.values()) { System.out.println(status); } } }
Output
PENDING: The job is pending. IN_PROGRESS: The job is currently in progress. COMPLETED: The job has been completed. FAILED: The job has failed. CANCELLED: The job has been cancelled.
Example 3
public enum LoanStatus { PENDING(1, "Pending"), APPROVED(2, "Approved"), REJECTED(3, "Rejected"), DISBURSED(4, "Disbursed"), CLOSED(5, "Closed"), DEFAULTED(6, "Defaulted"); private final int id; private final String name; // Constructor LoanStatus(int id, String name) { this.id = id; this.name = name; } // Getter for the ID public int getId() { return id; } // Getter for the name public String getName() { return name; } @Override public String toString() { return name; } // Optional: Method to get LoanStatus by ID public static LoanStatus fromId(int id) { for (LoanStatus status : LoanStatus.values()) { if (status.getId() == id) { return status; } } throw new IllegalArgumentException("No status with ID " + id); } }
public class Main { public static void main(String[] args) { LoanStatus status = LoanStatus.APPROVED; // Print the loan status details System.out.println("Loan Status ID: " + status.getId()); System.out.println("Loan Status Name: " + status.getName()); // Retrieve and print status by ID LoanStatus statusById = LoanStatus.fromId(3); System.out.println("Loan Status by ID 3: " + statusById); } }
Output
Loan Status ID: 2 Loan Status Name: Approved Loan Status by ID 3: Rejected