----enumeration in the Java language

Source: Internet
Author: User

DAY08----Enumeration In the Java language

First, overview:

What is an enumeration? What is the function of enumerations? How do we use this enumeration. In fact, the enumeration is immutable member variables, such as: Spring and Autumn, year, 12 months, color and so on. Are some fixed constant values. is used to create pre-defined lists. Enumerations are and classes are of a level. It can be defined directly as a class, or it can be defined in a class. You can also define methods, variables, constructors, and so on in the definition enumeration.


Second, enumeration:

Example one:

        package www.com;                 //defines an enumeration.         enum weekday{    monday,tuesday, wenesday,thursday,friday,saturday,sunday        }         public class enum {    public static  void main (String[] args)  {//1, accessed by enumeration name. System.out.println (Weekday.monday); System.out.println (weekday.tuesday);//2, accessed by applying variables. You can only use your own data type to accept it. Other types are not possible. weekday wd = weekday.sunday; SYSTEM.OUT.PRINTLN (wd);//3, outputs the ordinal of this enumeration System.out.println (Weekday.FRIDAY.ordinal ()),//4, iterates through the enumeration, is returned as an array, So use arrays to accept them. Weekday wdy[] = weekday.values ();for  (Weekday weekday : wdy)  { System.out.print (weekday+ ",");}     }        } 

Third, concluding remarks:

We are done with our "enumeration" knowledge here. We know how to define an enumeration, how to use access to an enumeration, and how to iterate through an enumeration. You will also use it directly in the subsequent work. At least you have to be a stranger in the process of development.

This article from "Lonely One Night" blog, reproduced please contact the author!

----enumeration in the Java language

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.