Why enum classes in Java cannot have the public constructor

Source: Internet
Author: User

Disclaimer: This blog is an original blog. Without consent. No reprint! The original link is http://blog.csdn.net/bettarwang/article/details/27262809.

Starting with the enumeration class from Java 5, it is important to note that the class defined by the enum inherits the Java.lang.Enum class instead of the object class by default.

At the same time , note that an enumeration class cannot derive subclasses (the default modifier for a class is final). The reason for this is that it only has the private constructor . So why design it like this?

In fact very easy to make it clear that the so-called enumeration class is a special class that includes a fixed number of instances (and the value of the instance is also fixed), Assuming that it contains a public constructor, then the outside of the class can be created by the constructor to create a new instance, it is obvious that the number and value of the instance is not fixed, This contradicts the original intention of defining the enumeration class, in order to avoid such an image. The private adornment is used by default for constructors of enum classes. If you explicitly specify additional access controls for the constructor of the enumeration class, an error is compiled.

Other than that. Note All instances of an enumeration class must be explicitly listed in its first row, otherwise it cannot produce an instance.

For example, here is a classic demo sample that uses an enumeration class:   

Import Java.util.*;enum planet{  Mercury,venus,earth,mars,jupiter,saturn,uranus,neptune}public class EnumSample {Public   void FlyTo (Planet Planet)   {     String destination= "";     Switch (planet)     {case       Mercury:          destination= "Mercury";          break;       Case VENUS:          destination= "Venus";          break;       Case Earth:          destination= "the Globe";          break;       Case mars:          destination= "Mars";          break;       Case JUPITER:          destination= "Jupiter";          break;       Case SATURN:           destination= "Saturn";           break;       Case URANUS:           destination= "Uranus";           break;       Case NEPTUNE:           destination= "Neptune";           break;      }      System.out.println ("The destination is" +destination);  public static void Main (String[]args)  {     enumsample sample=new enumsample ();     Sample.flyto (Planet.neptune);   }}

Why enum classes in Java cannot have the public constructor

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.