How to define a constant (constant) in Java)

Source: Internet
Author: User

First, let's look at the example:

/** <Br/> * Method One <br/> */<br/> interface constantinterface {<br/> string Sunday = "Sunday "; <br/> string Monday = "Monday"; <br/> string Tuesday = "Tuesday"; <br/> string Wednesday = "Wednesday "; <br/> string Thursday = "Thursday"; <br/> string Friday = "Friday"; <br/> string Saturday = "Saturday "; <br/>}< br/>/** <br/> * method two <br/> */<br/> Enum constantenum {<br/> Sunday, Monday, tuesday, we Dnesday, Thursday, Friday, saturday <br/>}< br/>/** <br/> * method three <br/> */<br/> class constantclassfield {<br/> Public static final string Sunday = "Sunday "; <br/> Public static final string Monday = "Monday"; <br/> Public static final string Tuesday = "Tuesday "; <br/> Public static final string Wednesday = "Wednesday"; <br/> Public static final string Thursday = "Thursday"; <br/> Public static Final string Friday = "Friday"; <br/> Public static final string Saturday = "Saturday "; <br/>}< br/>/** <br/> * method four <br/> * http://www.ibm.com/developerworks/cn/java/l-java-interface/index.html <br/> */<br/> class constantclassfunction {<br /> Private Static final string Sunday = "Sunday "; <br/> Private Static final string Monday = "Monday"; <br/> Private Static final string Tuesday = "Tuesday "; <Br/> Private Static final string Wednesday =" Wednesday "; <br/> Private Static final string Thursday =" Thursday "; <br/> Private Static final string Friday = "Friday"; <br/> Private Static final string Saturday = "Saturday"; <br/> Public static string getsunday () {<br/> return Sunday; <br/>}< br/> Public static string getmonday () {<br/> return Monday; <br/>}< br/> Public static string gettuesday () {< Br/> return Tuesday; <br/>}< br/> Public static string getwednesday () {<br/> return Wednesday; <br/>}< br/> Public static string getthursday () {<br/> return Thursday; <br/>}< br/> Public static string getfirday () {<br/> return Friday; <br/>}< br/> Public static string getsaturday () {<br/> return Saturday; <br/>}< br/> public class testconstant {<br/> static final string day = "Saturday"; <br/> Pu BLIC static void main (string [] ARGs) {<br/> system. Out. println ("Is today Saturday? "); <Br/> system. out. println (Day. equalsignorecase (constantinterface. saturday); <br/> system. out. println (Day. equalsignorecase (constantenum. saturday. name (); <br/> system. out. println (Day. equalsignorecase (constantclassfield. saturday); <br/> system. out. println (Day. equalsignorecase (constantclassfunction <br/>. getsaturday (); <br/>}< br/>} 

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 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 details, refer to [1 ].

 

Reference

[1] http://www.ibm.com/developerworks/cn/java/l-java-interface/index.html

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.