Java Enum in 10 Minutes

This tutorial aims at teaching Java enums to newbies in 10 mins. To meet this goal I will keep my descriptions short and concise. References will be provided at the end of the tutorial for detailed study.

  • Concept 1 : Enums are introduced in Java 1.5 and are used to define constants.

Scenario 1: Defining Constants
public enum WeekDay{
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY,
    SUNDAY;
}


Usage
    WeekDay  day =  WeekDay.THURSDAY;
  1. System.out.println(day); --> Will print THURSDAY
  2. if(day == WeekDay.THURSDAY) { // do something }
  3. switch(day){ case MONDAY : //do something e.t.c}
  4. public void someMethod(WeekDay day){//do something with day}

  • Concept 2 : An Enum can be considered to be like a class and the constants in the enum can be considered to be instances (objects) of the class.
  • Concept  3: An Enum's constructor should always be private if not compile time error will be thrown.

Scenario 2: Defining Constants with a value and methods inside enums
public enum Level {
    MIN(10), MEDIUM(50), MAX(100);

    private int value;
    private Level(int value){
        this.value = value;
    }

   public int getValue(){ return value; }
}

In the above example, when an enum is created,  the private constructor is called and the value mentioned in the constant will be passed and stored in the int variable. The user has no way of changing the value thenceforth.

Usage
Level level = Level.MIN;
  1. System.out.println(level); --> Will Print MIN (to get MIN : 10 one alternative is to override the "toString()" method in the enum)
  2. System.out.println(level+":"+level.getValue()); --> we are calling a method on the enum here, this will output MIN:10

  • Concept 4 : Enums can implement interfaces and have abstract methods defined in them.
Scenario 3: Enums implementing interfaces and declaring abstract methods.

public interface SillyInterface{ public String saySomethingSilly();}

enum Advanced implements SillyInterface{
    //Each constant of the enum can be considered to be an instance of the class.
    //Each instance has to therefore implement abstract methods defined as well as override methods of
    //any interface that gets implemented.
    Funny("Jim Carry") {
        @Override
        String sayDialogue() {
            return "Did you miss meeeeeeeeeeeeeeeeee";
        }
        public void saySomethingSilly(){return this.getName()+" says some silly
        thing";}
    },
    BAD("Gabbar Singh") {
        @Override
        String sayDialogue() {
            return "Kitne Aadmi the";
        }
     public void saySomethingSilly(){return this.getName()+" says some silly
     thing";}
    };
    private String name;
    private advanced(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    //This abstract method has to be implemented by each instance of the enum.
    abstract String sayDialogue();
}

Thats it. Now you know most of the enum related details.

References:





Comments

arjun said…
Very nice article and ref list, I would like to share my references too
http://www.javaworld.com/javaworld/javatips/jw-javatip122.html
http://tutiez.com/enum-in-java-with-java-enum-examples.html
http://chaoticjava.com/posts/tricks-with-enums/
Thanks Arjun for sharing these links.

Popular posts from this blog

Solved: Fix for Git clone failure due to GnuTLS recv error (-9)

QuickBite: Tap Vs Veth

Openstack : Fixing Failed to create network. No tenant network is available for allocation issue.