An enum is a shorthand form of a enumeration (enumeration) that is contained in a Java.lang package. Familiar with c,c++,c#, or Pascal should have an understanding of the list, first look at an example:
Public enum Season {winter, Spring, Summer, fall}
An enum is an object that defines a set of values, and it can include 0 or more value members. It is of type Enum and cannot have two or more identical properties or values in an Enum object. The Java programmers before the time are usually enumerated using the method of the interface, such as:
Public interface Season {
static winter = 0;
static spring = 1;//etc ...
}
The Java enumeration that introduces the enum is much easier to write, and it is only necessary to define an enum-type object. The value of the Enum object automatically gets a numeric value, starting at 0, incrementing sequentially. Look at a simpler example of an enum implementation:
Enumdemo.java
Package net.javagarage.enums;
/*
We can loop over the values we put into the enum
Using the values () method.
Note This enum seasons is compiled into a
Separate unit, called Enumdemo$seasons.class
*/
public class Enumdemo {
/*declare the enum and add values to it. Note So, like in C #, we don ' t use A;
End this statement and we use commas to separate the values * *
Private enum Seasons {winter, spring,
Summer, Fall}
List the values
public static void Main (string[] args) {
For (Seasons S:seasons.values ()) {
System.out.println (s);
}
}
}
Run the above code and you get the following results:
Winter
Spring
Summer
Fall