Enum Types in Java

Java provided Enum types are very powerful than other programming languages. Following are the main points one should remember about java enum types.

  • enum type is defined using enum keyword
  • enum type is a special data type that allows a variable that consist of a predefined constants
  • enum declaration defines a class
  • enum class body can include constructors, methods and fields. Compiler automatically adds some special methods when it creates an enum
  • enum constants are implicitly static and final
  • enum internally extends Enum class. enum may implement several interfaces but can’t extend any class
  • enum improves type safety
  • java implicitly includes values() method when it creates an enum. The values() method returns an array containing all the values of the enum
  • It is available from jdk 1.5 onward

Following is the simple example which uses enum type variables and in main method list is retrieve and printed on console.

Output of this program is listed below:

Another program to define your own method and initialize enum constant variables.

Output of this program is listed below:

Video Tutorial

Leave a Reply

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