Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一堂课:
public class Vars { public static final String NAME = "Alex"; public static final String RANK = "COLONEL"; }
在我的活动中,我做:
doSomething(Vars.NAME); doSomething(Vars.RANK);
这是对的吗?
谢谢。
是的,它应该。通常还建议将所有常量放在一个类中,因为这样可以更轻松地维护所有常量,更不用说减少跨类的常量复制了。
如果您询问从另一个类访问公共静态变量,是的,您正在 100% 编写代码。
您可以使用直接访问公共静态变量className.variableName
className.variableName