Java Constants Grouping

Source: Internet
Author: User
Tags constant final naming convention

Since all fields that place an interface automatically have static and final properties, an interface is a good tool for grouping constant values, and it has a very similar effect to a C or C + + enum. As shown in the following example:

: Months.java
//Using interfaces to create groups of constants
package c07;

Public interface Months {
  int
    January = 1, February = 2, March = 3, 
    APRIL = 4, May = 5, JUNE = 6, July = 7,
   august = 8, September = 9, October = ten,
    November = one, December =,///:~

Note According to the Java naming convention, the static final base data type with a fixed identifier (that is, the compile-time constant) takes all uppercase letters (the underscore separates multiple words in a single identifier).
The fields in the interface automatically have public properties, so there is no need to specify them specifically.
Now, by importing c07.* or c07. Months, we can use constants from outside the package--just as you would any other package. In addition, you can reference a value with an expression similar to months.january. Of course, we get just an int, so we don't have the extra type of security like the C + + enum. But this (commonly used) technology has undoubtedly been a huge improvement over the number of hard-coded (hard coded) numbers into their own programs. We usually refer to the behavior of "hard-coded" numbers as "magic numbers", which produce code that is very difficult to maintain.
If you do not want to discard additional type security, you can build a class such as the following (comment ①):

//: Month2.java//A More robust enumeration System package c07;
  Public final class Month2 {private String name;
  Private Month2 (String nm) {name = NM;}
  Public String toString () {return name;} Public final static Month2 = new Month2 ("January"), FEB = new Month2 ("February"), MAR = new Month2 ("March "), APR = new Month2 (" April "), may = new Month2 (" may "), June = new Month2 (" June "), and the = new Month2 (" July ") , AUG = new Month2 ("August"), SEP = new Month2 ("September"), OCT = new Month2 ("October"), NOV = new Month2
  ("November"), DEC = new Month2 ("December");
  Public final static month2[] month = {A. N., FEB, MAR, APR, May, June, April, AUG, SEP, OCT, NOV, DEC};
    public static void Main (string[] args) {Month2 m = Month2.jan;
    System.out.println (m);
    m = month2.month[12];
    System.out.println (m);
    System.out.println (M = = Month2.dec);
  System.out.println (M.equals (MONTH2.DEC)); }
} ///:~

①: It was a rich Hoffarth e-mail that triggered the way I wrote the program.

This class is called Month2 because there is already a month in the standard Java library. It is a final class and contains a private builder, so no one can inherit from it, or make an instance of it. The only instance is the final static objects, which are created inside the class itself, including: Jan,feb,mar, and so on. These objects are also used in the month array, which allows us to select the month by number instead of by name (note that an extra one is provided in the array, which increases the offset by 1, and makes December indeed December). In main (), we notice the security of the type: M is a Month2 object, so it can only be assigned to MONTH2. In the previous Months.java example, only the int value was provided, so the int variable that was intended to represent a month might actually get an integer value, which might not be very secure.
The method described here also allows us to exchange the use of = = or equals (), as shown in the rear of main ().

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.