在Java中,常量的可见性是由它们的访问修饰符决定的。Java中有四种访问修饰符:public、protected、private和默认(没有修饰符)。
package com.example;
public class Constants {
public static final String MY_CONSTANT = "SomeValue";
}
package com.example;
public class Constants {
protected static final String MY_CONSTANT = "SomeValue";
}
package com.example;
public class Constants {
private static final String MY_CONSTANT = "SomeValue";
public static String getMyConstant() {
return MY_CONSTANT;
}
}
package com.example;
public class Constants {
static final String MY_CONSTANT = "SomeValue";
}
总之,要使一个常量在不同包中可见,你需要将其声明为public或protected。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: Java调用存储过程事务问题怎么解决