Enum in Java

Source: Internet
Author: User

//Use an enumeration class instead of a static constant to
Enum classes are no longer inherited, which is final
Public
enumTestenum {//enumeration values appear in the first place, static constantsRED (255,0,0), BLUE (0,0,255), GREEN (0,255,0); //provides a private constructor PrivateTestenum (intRvintGvintBV) { This. Redvalue =RV; This. Greenvalue =GV; This. Bluevalue =BV; } //Custom data fields Private intRedvalue; Private intGreenvalue; Private intBluevalue; Public intGetredvalue () {returnRedvalue; } Public intGetgreenvalue () {returnGreenvalue; } Public intGetbluevalue () {returnBluevalue; }}

All enum classes inherit the method of the enum
(1) ordinal () method: Returns the order of enumeration values in the enumeration class. This order depends on the order in which the enumeration values are declared.
Color.RED.ordinal (); Results returned: 0
Color.BLUE.ordinal (); Results returned: 1
(2) CompareTo () method: Enum implements the Java.lang.Comparable interface, so you can compare the order of objects like the specified object. The CompareTo in the enum returns the difference in order of the two enumeration values. Of course, the premise is that two enumeration values must belong to the same enumeration class, otherwise a classcastexception () exception will be thrown. (Visible source code)
Color.RED.compareTo (Color.Blue); return result-1
(3) The values () method: A static method that returns an array that contains all the enumeration values.
Color[] Colors=color.values ();
for (Color c:colors) {
System.out.print (c+ ",");
}//return Result: Red,blue,black Yellow,green,
(4) ToString () method: Returns the name of the enumeration constant.
Color c=color.red;
System.out.println (c);//return Result: RED
(5) ValueOf () Method: This method is relative to the ToString method and returns an enumeration constant of the specified enumeration type with the specified name.
Color.valueof ("BLUE"); return Result: Color.Blue
(6) Equals () method: Compares references to two enum class objects.

Enum in Java

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.