Usage of the new Java 1.5 feature enum

Source: Internet
Author: User
Tags numeric value

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.