Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

Simple Java Enum Example

$
0
0

enum_data_type - Crunchifiy

Enums are lists of constants. When you need a predefined list of values which do not represent some kind of numeric or textual data, you should use an enum.

This is very simple Enum Example. Nothing special.

package com.crunchify.tutorials;

/**
 * @author Crunchify.com
 */

public enum CrunchifyEnumCompany {

	GOOGLE("G"), YAHOO("Y"), EBAY("E"), PAYPAL("P");

	private String companyLetter;

	private CrunchifyEnumCompany(String s) {
		companyLetter = s;
	}

	public String getCompanyLetter() {
		return companyLetter;
	}
}

package com.crunchify.tutorials;

import com.crunchify.tutorials.CrunchifyEnumCompany;

/**
 * @author Crunchify.com
 */

public class CrunchifyEnumExample {

	public static void main(String[] args) {
		System.out.println("Get enum value for Comapny 'eBay': "
				+ CrunchifyEnumCompany.EBAY.getCompanyLetter());
	}

}

Output:

Get enum value for Comapny 'eBay': Value: E

If you are looking for detailed tutorial of Enum then please visi this: http://crunchify.com/why-and-for-what-should-i-use-enum-java-enum-examples/

List of all Java Examples.

The post Simple Java Enum Example appeared first on Crunchify.


Viewing all articles
Browse latest Browse all 1037

Trending Articles