Learn Java enums by Example

Easy to learn Java Enums with the help of simple examples

Example 1:

package LearnEnum;

/*
 * Declare Cards0 in Cards0.java file.
 */

public enum Cards0 {
	VISA, MASTER, AMEX, DISCOVER
}

Example 2:

package LearnEnum;

import java.util.EnumSet;

/**
 *
 * Declare enum inside class. We can't declare inside the method.
 *
 */

public class Cards1 {

 public enum CreditCards {
 VISA, MASTER, AMEX, DISCOVER
 }

 public static void main(String[] args) {

 for (CreditCards cards : CreditCards.values()) {
 System.out.println(cards.name());
 }

 System.out.println("------EnumSet.range------");
 for (CreditCards d : EnumSet.range(CreditCards.VISA, CreditCards.AMEX))
 System.out.println(d);
 }
}

Result:
VISA
MASTER
AMEX
DISCOVER
------EnumSet.range------
VISA
MASTER
AMEX

Example 3:

package LearnEnum;

/**
 * enum can implement other classes.
 *
 * enum implicitly implements java.io.Serializable and java.lang.Comparable
 *
 */

public class Cards2 {

 public enum CreditCards implements Runnable {
 VISA, MASTER, AMEX, DISCOVER;

 public void run() {
 System.out.println(this.name());

 }
 }

 public static void main(String[] args) {

 for (CreditCards cards : CreditCards.values()) {
 cards.run();
 }
 }
}

Result:
VISA
MASTER
AMEX
DISCOVER

Example 4:

package LearnEnum;

/**
 *
 * enum with parameters
 *
 */

public class Cards4 {

	public enum CreditCards {
		VISA(1111), MASTER(2222), AMEX(3333), DISCOVER(4444);

		int iStartValue;

		private CreditCards(int iGiven) {
			System.out.println("initialization-->" + this.name() + " must start with " + iGiven);
			iStartValue = iGiven;
		}

		public int getStartValue() {
			return iStartValue;
		}

	}

	public static void main(String[] args) {

		for (CreditCards cards : CreditCards.values()) {
			System.out.println(cards.name()+"-->"+cards.getStartValue());
		}

	}
}

Result:
initialization-->VISA must start with 1111
initialization-->MASTER must start with 2222
initialization-->AMEX must start with 3333
initialization-->DISCOVER must start with 4444
VISA-->1111
MASTER-->2222
AMEX-->3333
DISCOVER-->4444

Example 5:

package LearnEnum;

/**
 *
 * enum with method declarations
 *
 */

public class Cards5 {

	public enum CreditCards {
		VISA {
			boolean validate(String number) {
				if (number.substring(0, 4).equals("1111"))
					return true;
				else
					return false;
			}
		},
		MASTER {
			boolean validate(String number) {
				if (number.substring(0, 4).equals("2222"))
					return true;
				else
					return false;
			}
		},
		AMEX {
			boolean validate(String number) {
				if (number.substring(0, 4).equals("3333"))
					return true;
				else
					return false;
			}
		},
		DISCOVER {
			boolean validate(String number) {
				if (number.substring(0, 4).equals("4444"))
					return true;
				else
					return false;
			}
		};

		abstract boolean validate(String iGiven);

	}

	public static void main(String[] args) {

		for (CreditCards cards : CreditCards.values()) {
			System.out.println(cards.name() + "==>"
					+ cards.validate("1111123412341234"));
		}

	}
}

Result:
VISA==>true
MASTER==>false
AMEX==>false
DISCOVER==>false

Reference:
http://java.sun.com/javase/6/docs/api/java/util/EnumSet.html
http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html
-o-

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s