Quick Expense Manger. Your free expense manager. Lots of features. The application is also ad free.

The Java Enums

Posted on Feb. 25, 2018
java-enums

In this post I will discuss the power of Java enums. Java enums are very powerful. Generally most java developers are not aware of the full potential of the Java enums. I will show you how Java enums weigh much higher over int constants.


Let's have a class with int constants that define power of the Chess pieces.


class ChessPiece{
public static final int QUEEN = 9;
public static final int ROOK = 5;
public static final int KNIGHT = 3;
public static final int PAWN = 1;

public static void pieceDescription(int piece){
switch(piece){
case QUEEN: System.out.println("I am the most powerful piece with estimated power
of 9 points"); break;
case KNIGHT: System.out.println("I am called Knight with estimated power of 3
points"); break;
case ROOK: System.out.println("I am called Rook with estimated power of 5
points"); break;
case PAWN: System.out.println("I am called Pawn with estimated power of 1 point");
break;
}
}
}


This is fine. But it's very unsafe. Because even if you are doing some complete nonsense, you don't get any warning from the compiler. To give an example, suppose you have a different variant of chess called FairyChess.


class FairyChess{
public static final int QUEEN = 8;
public static final int ROOK = 4;
public static final int KNIGHT = 2;
public static final int PAWN = 1;
}

Now if we by mistake do a comparison as below


if(ChessPiece.QUEEN == FairyChess.QUEEN){
//Do something
}

the compiler does not complain even if we have never meant to do this. Another thing to note is I have forgot to put the piece BISHOP in my ChessPiece class. But the BISHOP has the same power as the KNIGHT. So, I am in trouble now.


Because I cannot simply put the BISHOP in the switch statement as BISHOP and KNIGHT has the same value and switch statement will not work as intended.


So, I will have to somehow re-encode all the values now to differentiate the KNIGHT from the BISHOP and tell everyone to follow the same convention. This is not flexible at all.


Java enums to the rescue. At first glance Java enums look like the int constants. But appearances are deceiving. Java enums are actually full fledged generalized singletons.


We can achieve the above as shown in the code below.


public enum ChessPiece {
QUEEN(9),
KNIGHT(3),
BISHOP(3),
PAWN(1),
ROOK(5);

private final int power;
private ChessPiece(int power){
this.power = power;
}

public int getPiecePower(){
return power;
}

public void showPieceDesc(ChessPiece piece){
switch(piece){
case QUEEN: System.out.print("I am Queen with power of 9 points");break;
case KNIGHT: System.out.print("I am Knight with power of 3 points");break;
case BISHOP: System.out.print("I am Bishop with power of 3 points");break;
case PAWN: System.out.print("I am Pawn with power of 1 point");break;
case ROOK: System.out.print("I am Rook with power of 5 points");break;
}
}
}


Now if we have a different FairyChess enum as below


public enum FairyChess{
QUEEN,KNIGHT,BISHOP,PAWN,ROOK;
}

Now if we try to do the below comparision


if(ChessPiece.QUEEN == FairyChess.QUEEN){
//Do something
}


The compiler will not allow this.


We can also have better safety by declaring the showPieceDesc() method abstract and implementing it when we add a new piece. In this way we will be forced to implement the showPieceDesc for every piece we add and thus eliminating any chance of forgetting to add the description in the switch statement. We can rewrite the ChessPiece enum as below to achieve this.


public enum ChessPiece {

QUEEN(9) { public void desc(){System.out.print("I have power 9 and I am the Queen");}},
KNIGHT(3) { public void desc(){System.out.print("I have power 3 and I am Knight");}},
BISHOP(3) { public void desc(){System.out.print("I have power 3 and I am Bishop");}},
PAWN(1){ public void desc(){System.out.print("I have power 1 ans I am Pawn");}};
ROOK(5){ public void desc(){System.out.print("I have power 5 ans I am Rook");}};

private final int power;

private ChessPiece(int power) {
this.power = power;
}

public int getPower() {
return this.power;
}

public abstract void desc();

}


For each value declared inside the enum, Java creates a Singleton instance for that value which comes with all the facilities inherited from the Object class such as toString() and compareTo() etc.


So, in short enums are great. It relieves us from whole lot of rewiring and reinvention of the wheel.


Sharing is Caring!

Quick Expense Manger. Your free expense manager. Lots of features. The application is also ad free.

GET FREE UPDATES


RECOMMENDED POSTS FOR YOU


profile image

Kaushik Baruah


ABOUT

My name is Kaushik Baruah and I am the chief blogger on this Blog. I have developed this Blog from scratch using Django as the backend and here I like to share my experience as software engineer and research engineer with my online readers. I will try to focus on career planning, latest emerging technologies and tutorials on various computer science subjects. You can follow me on Twitter, Facebook and Google+

GET FREE UPDATES

POPULAR POSTS

Copyright © 2018
About Us

My name is Kaushik Baruah and I am the chief blogger on this Blog. I have developed this Blog from scratch using Django as the backend and here I like to share my experience as software engineer and research engineer with my online readers. I will try to focus on career planning, latest emerging technologies and tutorials on various computer science subjects.

Get Free Updates