Java Enum

So, in Java 5, they added language support for enumeration types. There's a Java Trails tutorial that does a good job, but it left out one important bit, so here goes.

The less good way to do an enumeration is manaully, without language support.

public class Colors { 
        private static final int RED = 1; 
        private static final int BLUE = 2; 
        private static final int YELLOW = 3; 
} 
Which would be used like: 
public boolean compareColors(int color) { 
        if (color == Colors.RED) { 
                return true; 
        } else { 
                return false; 
        } 
} 
There are a bunch of problems. The two biggest
- It doesn't have type safety; anyone could call the method with any integer, not necessarily one that meant a color. Those kind of coding mistakes are hard to find.
- Printing out the value later doesn't help us much; System.out.println(Colors.RED) just returns "1". There's no useful context there.


So, enter the Java enum type.
public enum Colors { 
        RED, BLUE, YELLOW 
} 

Which would be used like:
public boolean compareColors(Colors color) { 
        if (someVariable == Colors.RED) { 
                return true; 
        } else { 
                return false; 
        } 
} 

We've gained type safety; we *know* that whatever was passed to the method .compareColors was a color; the code won't compile if a developer makes that mistake.

We've also gained context for printing/debugging; the enum type has a default .toString() method. (This is the important part the Java Trails Tutorial is missing.)

The code:
System.out.println(Colors.RED); 
Will display:
RED 


So, that's the simple enumeration. The next more complex step? Each of those RED, GREEN, BLUE values can have attributes, and also have getters and setters.
public enum Colors { 
                RED("Red"), 
                BLUE("Blue"), 
                YELLOW("Yellow"); 
                
                private Colors(String description) { 
                        this.description = description; 
                } 
                private final String description; 
                
                @Override 
                public String toString() { 
                        return description; 
                } 
        } 
So now, the code:
System.out.println(Colors.RED); 
Will display:
Red 

But that might not seem as useful, so here's a more logical example:
public enum Colors { 
                RED("Red", "#FF0000"), 
                BLUE("Blue", "#00FF00"), 
                YELLOW("Yellow", "#0000FF"); 
                
                private Colors(String description, String rgb) { 
                        this.description = description; 
                        this.rgb = rgb; 
                } 
                private final String description; 
                private final String rgb; 
                
                public String getRGB() { 
                        return rgb; 
                } 
                
                @Override 
                public String toString() { 
                        return description; 
                } 
        } 
The code:
System.out.println(Colors.RED); 
Will still return:
Red 
But also, the code:
System.out.println(Colors.RED.getRGB()); 
Will return
#FF0000 

Which would be useful at times. As an important note, even if toString() returns the same value (or same value references!) as another toString(), Colors.RED will not be equal to Colors.BLUE; they're always separate, even if they have the same values inserted.

1 comment:

Etisbew said...

Offshore Outsourcing Software Development Company Etisbew - We have got a great information on your blog, please keep posting such information in future.