Java Advanced Knowledge Summary __java

Source: Internet
Author: User

Data collated in the network: reference http://blog.csdn.net/column/details/java0115.html, thank you here 1. About the use of enumerations

The enum enum type in Java has many unexpected uses, so let's take a look at it here. 1, you can add variables and methods in the enum

Let's take a look at some code examples:[Java]  View plain copy public enum state {            normal ("normal",  1),  update ("updated",  2),  deleted ("deleted",  3),  fired ("masked",  4 );           //  member variables             private String name;            private int index;              //   Construction method, note: The construction method cannot be public because an enum cannot be instantiated            private  state (string name, int index)  {                this.name = name;                this.index = index;            }              //  General methods             public static string getname (Int index)  {                for  (state c  : state .values ())  {                    if  (C.getindex ()  == index)  {                         return c.name;                    }               }                return null;            }                get set  Method            public string  getname ()  {                return name;           }               public void setname (String name)  {               this.name = name;            }               public int getindex ()  {                return index;           }    &NBsp;          public void setindex (Int index)  {                this.index =  index;           }       }   

From the above code we can see that the enumeration values are defined, followed by a semicolon, and then other variables and methods can be defined. In addition, it is necessary to note that the constructor method in the enum cannot be identified by public, in order to prevent the user from instantiating the enum. 2, can be used to define constants

Let's review how to define constants in Java, and look at the following code:

public static final int normalstate = 1;
private static final int updatestate = 2;

We can also use the Enum enumeration to replace the above constant definition, the following code: [Java] view plain copy

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.