Java – Enum to String Value

name() or toString() option on enum value can give string representation of the enum

 

package com.pwn.enm;

public class EnumToString {

	enum DAYS {
		SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
	}

	public static void main(String[] args) {
		/*
		 * name() is an internal method in enum that returns the String representation
		 * of the enum, the return String represents exactly how the enum value was
		 * defined.
		 */
		System.out.println(DAYS.SUNDAY.name());

		/* or Use toString() method */
		System.out.println(DAYS.FRIDAY.toString());

	}
}

SUNDAY
FRIDAY

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s