Java record-40-Best way to define Constants
In actual work development, constants are often used. Common constants, such as: public static final int AGE_OF_PERSON = 18; naming rules for constants: All words are in uppercase. If there are multiple words, connect them with underscores. When the final constant is declared in Java, the static keyword is added. Why? Since it is final immutable, there is no need to declare a variable for each instance object of the class, and the class can use a unique one, this is also consistent with the definition of constants. The static modifier is owned by the class and can be accessed using the class name. Constant: To facilitate and intuitively express some things and make the code readable. What other methods are used?
/** * Method One */interface ConstantInterface { String SUNDAY = "SUNDAY"; String MONDAY = "MONDAY"; String TUESDAY = "TUESDAY"; String WEDNESDAY = "WEDNESDAY"; String THURSDAY = "THURSDAY"; String FRIDAY = "FRIDAY"; String SATURDAY = "SATURDAY";}/** * Method Two */enum ConstantEnum { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}/** * Method Three */class ConstantClassField { public static final String SUNDAY = "SUNDAY"; public static final String MONDAY = "MONDAY"; public static final String TUESDAY = "TUESDAY"; public static final String WEDNESDAY = "WEDNESDAY"; public static final String THURSDAY = "THURSDAY"; public static final String FRIDAY = "FRIDAY"; public static final String SATURDAY = "SATURDAY";}/** * Method Four * http://www.ibm.com/developerworks/cn/java/l-java-interface/index.html */class ConstantClassFunction { private static final String SUNDAY = "SUNDAY"; private static final String MONDAY = "MONDAY"; private static final String TUESDAY = "TUESDAY"; private static final String WEDNESDAY = "WEDNESDAY"; private static final String THURSDAY = "THURSDAY"; private static final String FRIDAY = "FRIDAY"; private static final String SATURDAY = "SATURDAY"; public static String getSunday() { return SUNDAY; } public static String getMonday() { return MONDAY; } public static String getTuesday() { return TUESDAY; } public static String getWednesday() { return WEDNESDAY; } public static String getThursday() { return THURSDAY; } public static String getFirday() { return FRIDAY; } public static String getSaturday() { return SATURDAY; }}public class TestConstant { static final String day = "saturday"; public static void main(String[] args) { System.out.println("Is today Saturday?"); System.out.println(day.equalsIgnoreCase(ConstantInterface.SATURDAY)); System.out.println(day.equalsIgnoreCase(ConstantEnum.SATURDAY.name())); System.out.println(day.equalsIgnoreCase(ConstantClassField.SATURDAY)); System.out.println(day.equalsIgnoreCase(ConstantClassFunction .getSaturday())); }}
Method 1 adopts the property of static final by default in the Interface variable. Method 2 uses the Enum type introduced in Java 5.0. Method 3 adopts the method of modifying variables using static final in common classes. Method 4 is similar to method 3, but a constant is obtained through a function. First, defining global variables seems to be in violation of Java's object-oriented encapsulation feature and added coupling. Therefore, the best way is to avoid defining global variables. If it is a parameter, you can write it into the configuration file. If it is necessary, method 2 is the most recommended. Method 3 is something that everyone can think of and is very intuitive. Method 1 and method 3 are essentially the same. Method 4 provides flexibility. For more information, see references.
Next article: http://www.bkjia.com/kf/201510/447446.html