Java enumeration types

Source: Internet
Author: User

The enumeration type is a new feature of JDK1.5. Obviously, the enum is very much like a special class, in fact the type of the enum declaration definition is one. These classes are subclasses of the Enum class in the class library (java.lang.enum<e>).  They inherit many of the useful methods in this enum. Example:
1  Public enumColor {2  3Red ("Red"), Blue ("blue"), White ("whites"), Black ("Black"), Yello ("yellow");//These enumeration values are of the type of public static final.4  5  6     PrivateString color;//Custom Data type public,protected,private types can be, private here to encapsulate7  8     PrivateColor (String color) {//The constructor can only be private, and there is absolutely no public constructor allowed. 9          This. color =color;Ten     } One   A } -   - classmain{ the      Public Static voidMain (string[] args) { -Color color =Color.Blue; -SYSTEM.OUT.PRINTLN (color);//blue,tostring () method: Returns the name of the enumeration constant.  -   +         intOrder = color.ordinal ();//Order = 1 Returns the sequence of enumeration definitions, calculated starting from 0 -   +color[] clolors = Color.values ();//returns an array containing all enumerations worth A          for(Color c:clolors) { at System.out.println (c); -         } -   -System.out.println (color.valueof ("RED")); -   -   in         Booleanrs = color.equals (Color.Blue);//compares a reference to two enum class objects.  -     } to}
The enumeration type is actually a class, but the enumeration type is also a very special class. 1. Its enumeration value (Red,blue ...) is a color class object (class instance):
And these enumeration values are public static final, which is the constant way that we often define, so the enumeration values in the enumeration class are best capitalized. 2. The enumeration class is class, of course, there are constructors, methods, and data fields in the enumeration type.    However, the constructor of an enumeration class is very different: (1) The constructor is called only when the enumeration value is constructed. (2) The constructor can only be private, and there is absolutely no public constructor allowed. This guarantees that external code cannot newly construct an instance of the enumeration class. This is perfectly reasonable, because we know that the enumeration value is a constant of public static final.  However, the methods and data fields of the enumeration class can allow external access.       3. All enum classes inherit the Enum method, which we'll describe in detail below. (1) ordinal () method: Returns the order of enumeration values in the enumeration class. This order is based on the order in which the enumeration values are declared, which are counted starting at 0.
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.
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 ();  // colors = {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");   //
(6) Equals () method: Compares references to two enum class objects. 4. Enumeration classes can be used in switch statements.
Color color=color.red;         Switch (color) {           case RED:System.out.println ("It ' s RED") ;  break;             Case BLUE:System.out.println ("It s Blue");  Break ;             Case BLACK:System.out.println ("It s Blue");  Break ;      }  
Summary: So when do we use enumerations? "Effective Java second Edition" is quoted here "whenever a fixed constant is required." Of course, this includes natural enumeration types, such as planets, days of the week, number of pieces, and so on. But it also includes what you know at compile time that it might be worth other collections, such as menu options, action codes, command line tags, and so on. Constant set merging of enumerated types does not always have to be the same. "Original link: http://blog.csdn.net/cilen/article/details/7262454

Java enumeration types

Related Article

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.