In actual work development, we often use constants.
Constants we commonly use, for example: publicstatic final int age_of_person =;
Naming rules for constants: All words are capitalized, and if there are multiple words, use an underscore to connect.
When you declare a final constant in Java , you add the static keyword, which is why? Since it is final and immutable, there is no need to declare a variable for each instance object of the class, which uses only one, which also conforms to the definition of the constant. the static modifier is owned by the class and is accessible using the class name.
The function of constants: In order to more convenient, intuitive expression of something, so that the readability of the code provided.
What are the other ways to use it?
/** * 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 ()  { &NBsp; 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 one uses an interface (Interface) in which the default is the static final attribute.
Method two uses the Enum type introduced in Java 5.0 .
Method three takes the method of using static final modifier variables in a normal class .
Method Four is similar to method three, but the function is used to get the constant.
The first definition of global variables seems to violate Java 's object-oriented encapsulation characteristics, increasing coupling. So the best way is to avoid defining global variables. If it is a parameter, etc., you can write to the configuration file. If it is really necessary, method two is the most recommended. Method Three is everyone can think of, very intuitive. Method One and method three are essentially the same. Method Four provides flexibility, specific references.
Java record -40-defining constants Best Practices