Posts Java Enum
Post
Cancel

Java Enum

I use enum when I know what instances/constants I need.

It also provides compile time error check which is a plus point.

  • Declaring enum
1
2
3
4
5
enum TestEnum{
    A,
    B,
    C
}; //Use of semicolon is optional so both above and below code will compile successfully. 
1
2
3
4
5
6
enum TestEnum{
    A,
    B,
    C
} 
//The above declaration could be done inside a class called TestEnum.java or inside any other class.
  • Example
1
2
3
public class Dummy{
        enum TestEnum{ A, B, C}
}

Enum cannot be declared in a method.

So the below code will not compile and will give “enum type must not be local” error.

1
2
3
4
5
public class Dummy{
         public void someMethod() {
                enum TestEnum{ A, B, C}; //error
         }
}

We cannot call enum constructor The below code will not compile

1
2
3
4
5
6
public class Dummy{
         enum TestEnum{ A, B, C};
         public void someMethod() {
                TestEnum enum = new TestEnum(); //error        
         }
}
  • Using enum
1
2
3
4
5
6
public class Dummy{
         enum TestEnum{ A, B, C};
         public void someMethod() {
                TestEnum enum = TestEnum.A;      
         }
}
  • Enum constructor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
enum TestEnum{
    A(10),
    B(20),
    C(30);
   private final int variableValue;

   TestEnum(int variableValue) {
          this.variableValue = variableValue;
   }
   public int getValue(){
         return variableValue;
   }
    public String getType(){
         return "common type";
    }
} 

//getValue method can be renamed to whatever_name_you_like.
  • Using the above enum
1
2
3
4
5
6
7
8
9
10
11
12
public class Dummy{
         public void someMethod() {
                System.out.println("A value is "+TestEnum.A.getValue());  
                System.out.println("A type is "+TestEnum.A.getType()); // this will return "common type"
  
                System.out.println("B value is "+TestEnum.B.getValue());  
                System.out.println("B type is "+TestEnum.B.getType()); // this will return "common type"

                System.out.println("C value is "+TestEnum.C.getValue());  
                System.out.println("C type is "+TestEnum.C.getType()); // this will return "common type" 
         }
}
  • Customizing enum get value response

In the above example I created a common getType response. But lets assume for type “C” we want a “special type” response.

Now without adding any if-else logic this can be achieved by using special logic called “constant specific class body”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
enum TestEnum{
    A(10),
    B(20),
    C(30){
        public String getType(){
             return "special type";
        }
    };

   private final int variableValue;

   TestEnum(int variableValue) {
          this.variableValue = variableValue;
   }
   public int getValue(){
         return variableValue;
   }
    public String getType(){
         return "common type";
    }
} 
  • Using the above enum
1
2
3
4
5
6
7
8
9
10
11
12
13
public class Dummy{
         public void someMethod() {
                System.out.println("A value is "+TestEnum.A.getValue());  
                System.out.println("A type is "+TestEnum.A.getType());   // this will return "common type"
  
                System.out.println("B value is "+TestEnum.B.getValue());  
                System.out.println("B type is "+TestEnum.B.getType());    // this will return "common type"

                System.out.println("C value is "+TestEnum.C.getValue());  
                System.out.println("C type is "+TestEnum.C.getType());    // this will return "special type"
         }
}

enum static method values()

every enum has a static method called “values”, this returns an array of constants defined in that enum. Example :- using the above enum. I can replace the above print statements with a for each loop.

1
2
3
4
5
6
7
8
public class Dummy{
         public void someMethod() {
                 for (TestEnum testEnum : TestEnum.values()) {
                       System.out.println(testEnum.toString() + " value is " + testEnum.getValue());
                       System.out.println(testEnum.toString() + " type is " + testEnum.getType());
                 }
         }
} //enum "toString()" will return the constant variable name.

The above code “testEnum.toString()” will print :-

“A” then for next enum “B” then for next enum “C”.

As you can see from above the TestEnum has three constants names “A”, “B”, “C”, which is exactly what toString() method returns.

That’s it :)

This post is licensed under CC BY 4.0 by the author.