Java enum explained
This article teaches you how to use an enum in Java. Code examples help you to understand the enumeration syntax.
Tutorial info:
| Name: | Java enum explained |
| Total steps: | 1 |
| Category: | Basics |
| Date: | 2011-04-09 |
| Level: | Beginner |
| Product: | See complete product |
| Viewed: | 4745 |
Bookmark Java enum explained
Step 1 - Java enum usage explained
Java enum explained
Prior Java 1.5 there was no real enums in the language. It was able to imitate the enum functionality but it was not type safe and there was no namespace to make the code more readable.
With Java 1.5 finally enum was arrived. You can simply define an enumeration using the enum keyword and the following syntax:
After you have defined an enum you can simply use it as any other type as you can see in this code example as well:
Code:
public class EnumTest {
public enum Color { BLACK, BLUE, GREEN, YELLOW, WHITE }
public static Color getColor(Color c) {
System.out.println("The color is:" + c);
return c;
}public static void main(String[] args) {
Color myColor = getColor(Color.GREEN);
if (myColor == Color.GREEN) {
System.out.println("It is green!");
}}}
Tags: java enumeration, java enum, enum usage, how to use enum, java, enumeration, enum
| Java enum explained - Table of contents |
|---|
| Step 1 - Java enum usage explained |