「Java」インタフェースのfinal staticメンバー定数を使用するサンプル

構文
インタフェース名.メンバー変数(static final)
使用例
1.インタフェースの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.java.it;
public class InterFaceSam {
public static final int MAX_VAL = 1200;
public static final String HP_NAME = "arkgame.com";
}
package com.arkgame.study.java.it; public class InterFaceSam { public static final int MAX_VAL = 1200; public static final String HP_NAME = "arkgame.com"; }
package com.arkgame.study.java.it;

public class InterFaceSam {
      public static final int MAX_VAL = 1200;
      public static final String HP_NAME = "arkgame.com";
}

2.実行確認クラス

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.java.it;
public class SkillDemo {
public static void main(String[] args) {
int count = 1100;
String strA = InterFaceSam.HP_NAME;
if (count > InterFaceSam.MAX_VAL) {
System.out.println("比較結果: count > MAX_VAL");
} else {
System.out.println("比較結果: count < MAX_VAL");
}
System.out.println("文字列: " + strA + " 長さ: " + strA.length());
}
}
package com.arkgame.study.java.it; public class SkillDemo { public static void main(String[] args) { int count = 1100; String strA = InterFaceSam.HP_NAME; if (count > InterFaceSam.MAX_VAL) { System.out.println("比較結果: count > MAX_VAL"); } else { System.out.println("比較結果: count < MAX_VAL"); } System.out.println("文字列: " + strA + " 長さ: " + strA.length()); } }
package com.arkgame.study.java.it;

public class SkillDemo {

      public static void main(String[] args) {
            int count = 1100;
            String strA = InterFaceSam.HP_NAME;
            if (count > InterFaceSam.MAX_VAL) {
                  System.out.println("比較結果: count > MAX_VAL");
            } else {
                  System.out.println("比較結果: count < MAX_VAL");
            }
            System.out.println("文字列: " + strA + " 長さ: " + strA.length());
      }

}

3.実行結果
比較結果: count < MAX_VAL
文字列: arkgame.com 長さ: 11

Java

Posted by arkgame