Java enum class

Source: Internet
Author: User

Sometimes, the value of a variable is only within a finite set.

For example: the size of the pizza is only small, medium, large and oversized in these four sizes. Of course, these dimensions can be encoded as 1, 2, 3, 4, or S, M, L, X, respectively. But there are some hidden dangers. It is quite possible to save an erroneous value (such as 0 and m) in a variable.

For this scenario, you can customize the enumeration type. Enumeration types include a limited number of named values. For example

Enum Size{small,medium,large,extra_large};

You can now declare variables of this type:

Size s = size.medium;

A variable of size type can only be stored to an enumeration value given in this type declaration, or a null value, and NULL indicates that the variable has no value set.

Here is his full code plus running result:

Package Enums;import java.util.scanner;/** * Detailed see: Java Core Technology (original book tenth edition) p188-189 * @author Big lensure * */public class Enumtest {public static void Main (string[] args) {Scanner in=new Scanner (system.in); System.out.print ("Enter a size: (Small,medium,large,extra_large)"); String Input=in.next (). toUpperCase ();          Here to explain the toUpperCase method here, is to put your keyboard input letter formatted as uppercase, below the run figure size size=enum.valueof (size.class, input); System.out.println ("size=" +size); System.out.println ("abbreviation=" +size.getabbreviation ()), if (Size==size.extra_large) System.out.println ("Good Job--you paid attention to the _. ");} Enum Size{small ("S"), MEDIUM ("M"), Lareg ("L"), Extra_large ("XL");p rivate Size (String abbreviation) {this.abbreviation =abbreviation;} Public String getabbreviation () {return abbreviation;} private String abbreviation;}

The following for their own convenience after understanding, the detailed explanation:

public enum Size{small,medium,large,extra_large};

In fact, the type defined by this declaration is a class that has exactly four instances, where it is best not to construct new objects.

Therefore, when you compare the values of two enumerated types, never call equals, and use the "= =" directly.

If you want, you can add some constructors, methods, and fields to the enumeration type. Of course, the constructor is called only when constructing enumeration constants. A facet is an example:

public enum Size{small ("S"), MEDIUM ("M"), LARGE ("L"), Extra_large ("XL");p rivate String abbreviation;private Size ( String abbreviation) {this.abbreviation=abbreviation;} Public String getabbreviation () {return abbreviation;}}

All enum types are subclasses of the enum class. They inherit many of the methods of this class. One of the most useful is ToString, which returns the enumeration constant name. For example, Size.SMALL.toString () returns the string "SMALL".

The inverse method of ToString is static method valueof. For example, statements:

Size s = enum.valueof (Size.class, "SMALL");

Set S to Size.small.

Each enumeration type has a static values method that returns an array that contains all the enumerated values. For example, the following call

Size[] values = Size.values ();

Returns an array containing the element Size.small,size.medium,size.large,size.extra_large.

The ordinal method returns the position of the enumeration constant in the enum declaration, starting at 0. For example: Size.MEDIUM.ordinal () returns 1.

Code is actually the best embodiment of the knowledge of the content of the carrier, the above explanation actually does not play a big role, you read the code, also know a sorta.

These are the notes I saw in the tenth edition of Core Java Volume i-fundamentals.

The implementation details and principles of enumerations can also be consulted in the fourth edition of Thinkinginjava: http://blog.lichengwu.cn/java/2011/09/26/the-usage-of-enum-in-java/

Java enum class

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.