I was going through the enums in Java. It seems that the same things that enums can do can be achieved by collections or an array of Strings or user-defined types.
What is the main purpose of introducing enums in Java 5?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Advantages –
It is used for fields consist of a fixed set of constants.
Example is
Thread.Stateor
private enum Alignment { LEFT, RIGHT };You can restrict input parameter using
Enumlike-Here in align parameter could be only
Alignment.LEFTorAlignment.RIGHTwhich is restricted.Example of switch-case with
enum–