Login with  Log in with facebook
Hiring Manager? SIGN UP HERE
Apr/13

11

Enum in Java

 

For those of us who were coding extensive development in Java even before Java 1.5 know the pain faced before enum was introduced. The solution used to be Type-safe Enums which was a design pattern to simulate enum functionality as closely as possible. 

Introduction of enums changed things. Enums can be used wherever an Object can have only fixed number of States some examples being

  • Directions
  • Days of the Week
  • Primary Colors
  • Employee Types

public enum EmpType {

    FULL_TIME,PART_TIME,TEMP

}

Enums in java are different from other languages that they are fully functional classes. The advantage being that they can contain  methods and other fields. The compiler automatically creates some methods when it creates enum. e.g. there is static values() method that returns all the values of enums in the order they are created.

An enum implicitely extends java.lang.Enum.

switch case has also been retrofitted to support enums which is a big relief as it's tough to represent everything as ints.

Take a teaser test on enums in Java.

 

Schedule a Demo

Schedule a Demo with us

Name *
Email *
Phone *
Company *
Details