Class定义常量方法(推荐方法) //final修饰符 public final class Constants { //私有构造方法 private Constants() {} public static final int ConstantA = 100; public static final int ConstantB = 100; ...... } 采用“类.常量名”方法进行调用.需要私有化构造方法,避免创建该类的实例.同时不需让其他类继承该类. 如果多处需要访问工具类中定义的常量
接口 (A.java) : package config; public interface A { String PROJECT_ROOT_DIR = System.getProperty("user.dir"); } 类(B.java): (方法1) import config.A; public class B { public static void main(String[] args) { System.out.println(A.PROJECT_ROOT_DIR); }
一般的方式的使用静态代码块.比如: public final static Map map = new HashMap(); static { map.put("key1", "value1"); map.put("key2", "value2"); } 下面为一种简单定义Map常量的方式 public final static Map<String, Fragment> NAV_ITEM_ADPTER = new