Enum in Java

Posted on Updated on

Like any other keyword in Java, Enum is a keyword which was introduced in Java with JDK 5 releases. Enum are mostly used to define constants or well known as fixed variables in Java. It has some other popular uses, which have been discussed here.

Before introduction of Enum, Java classes are used to define constants but it had some serious drawbacks. There are some other advantages and uses of Enum apart from just defining constants. Below are some standard features of Enum.

1. Enums are type-safe means Enum has its own namesapce, we can’t assign any other value other than specified in Enum constants.

2. Enums are like any other class in Java with some exceptions, so we can define constructors, methods and variable inside Enum.

3. We can’t create any object of Enum outside of it, as constructors in Enums are private. So, values of enum constants specify at the creation time only.

4. Enum constants are implicitly static and final. So it can’t be changed once created.

5. We can use enum constants in switch statement and if condition using “==” operator. Refer here for more details.

6. Enum can’t extends any other class or Enum. But, it can implements interface to support multiple inheritance.

7. As Enum can implements interface, so it can override methods as well. For example-

8. Enum has valueOf(String str) method which converts String into Enum constant, which is very useful while working with Enum and String.

9. Enum has values() method which returns all constant of the enum.

Output

10. Order of constants are maintained in enum.

Some more useful links for Enum in Java are : Enum example, constructor overloading in Enum, uses of Enum.

3 thoughts on “Enum in Java

    Shardendu Shrikant said:
    April 29, 2016 at 12:44 pm

    Hi Team,

    Please add PL/SQL part also for database users.

    It would be a great help for us.

    Hope team will look into this point.

      Admin said:
      April 30, 2016 at 12:20 pm

      Hi Shardendu,

      Thank you for writing to us. We’ll definitely look into it.

    Shardendu Shrikant said:
    April 29, 2016 at 12:46 pm

    Hi Team,

    Very nice team work and very unique concept.

    Its really going to help many users to improve their skills.

    Great work done. Keep it up.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.