如何讓在JAVA中定義常量池

來源:互聯網
上載者:User

標籤:

首先看樣本:

  1. /** 
  2.  * Method One 
  3.  */  
  4. interface ConstantInterface {  
  5.     String SUNDAY = "SUNDAY";  
  6.     String MONDAY = "MONDAY";  
  7.     String TUESDAY = "TUESDAY";  
  8.     String WEDNESDAY = "WEDNESDAY";  
  9.     String THURSDAY = "THURSDAY";  
  10.     String FRIDAY = "FRIDAY";  
  11.     String SATURDAY = "SATURDAY";  
  12. }  
  13. /** 
  14.  * Method Two  
  15.  */  
  16. enum ConstantEnum {  
  17.     SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY  
  18. }  
  19. /** 
  20.  * Method Three 
  21.  */  
  22. class ConstantClassField {  
  23.     public static final String SUNDAY = "SUNDAY";  
  24.     public static final String MONDAY = "MONDAY";  
  25.     public static final String TUESDAY = "TUESDAY";  
  26.     public static final String WEDNESDAY = "WEDNESDAY";  
  27.     public static final String THURSDAY = "THURSDAY";  
  28.     public static final String FRIDAY = "FRIDAY";  
  29.     public static final String SATURDAY = "SATURDAY";  
  30. }  
  31. /** 
  32.  * Method Four 
  33.  * http://www.ibm.com/developerworks/cn/java/l-java-interface/index.html 
  34.  */  
  35. class ConstantClassFunction {  
  36.     private static final String SUNDAY = "SUNDAY";  
  37.     private static final String MONDAY = "MONDAY";  
  38.     private static final String TUESDAY = "TUESDAY";  
  39.     private static final String WEDNESDAY = "WEDNESDAY";  
  40.     private static final String THURSDAY = "THURSDAY";  
  41.     private static final String FRIDAY = "FRIDAY";  
  42.     private static final String SATURDAY = "SATURDAY";  
  43.     public static String getSunday() {  
  44.         return SUNDAY;  
  45.     }  
  46.     public static String getMonday() {  
  47.         return MONDAY;  
  48.     }  
  49.     public static String getTuesday() {  
  50.         return TUESDAY;  
  51.     }  
  52.     public static String getWednesday() {  
  53.         return WEDNESDAY;  
  54.     }  
  55.     public static String getThursday() {  
  56.         return THURSDAY;  
  57.     }  
  58.     public static String getFirday() {  
  59.         return FRIDAY;  
  60.     }  
  61.     public static String getSaturday() {  
  62.         return SATURDAY;  
  63.     }  
  64. }  
  65. public class TestConstant {  
  66.     static final String day = "saturday";  
  67.     public static void main(String[] args) {  
  68.         System.out.println("Is today Saturday?");  
  69.         System.out.println(day.equalsIgnoreCase(ConstantInterface.SATURDAY));  
  70.         System.out.println(day.equalsIgnoreCase(ConstantEnum.SATURDAY.name()));  
  71.         System.out.println(day.equalsIgnoreCase(ConstantClassField.SATURDAY));  
  72.         System.out.println(day.equalsIgnoreCase(ConstantClassFunction  
  73.                 .getSaturday()));  
  74.     }  
  75. }  

 

方法一採用介面(Interface)的中變數預設為static final的特性。

方法二採用了Java 5.0中引入的Enum類型。

方法三採用了在普通類中使用static final修飾變數的方法。

方法四類似方法三,但是通過函數來擷取常量。

首先定義全域變數似乎有違Java的物件導向的封裝特性,增加的耦合。所以最佳的方法是避免定義全域變數。如果是參數等,可以寫入設定檔。如果實在是必須的,方法二是最為推薦的。方法三是大家都能想到的,非常的直觀。方法一和方法三本質上一樣。方法四提供了靈活性,具體參考引用【1】。

如何讓在JAVA中定義常量池

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.