如何在 Java 中編寫常量名稱?


在編寫常量名稱時,建議

  • 所有字母都為大寫形式。
  • 如果常量包含多個單詞,它們之間應該用下劃線(_)分隔。

示例

即時演示

public class ConstantsTest {
   public static final int MIN_VALUE = 22;
   public static final int MAX_VALUE = 222;
   public static void main(String args[]) {
      System.out.println("Value of the constant MIN_VALUE: "+MIN_VALUE);
      System.out.println("Value of the constant MAX_VALUE: "+MAX_VALUE);
   }
}

輸出

Value of the constant MIN_VALUE: 22
Value of the constant MAX_VALUE: 222

更新於: 30-Jul-2019

623 瀏覽

開啟你的 事業

完成課程獲得認證

開始著手
廣告
© . All rights reserved.