我见过这样的例子:
public class MaxSeconds {
public static final int MAX_SECONDS = 25;
}
假设我可以有一个Constants类来包装常量,并将它们声明为静态final。我几乎不知道Java,我想知道这是否是创建常量的最佳方式。
我见过这样的例子:
public class MaxSeconds {
public static final int MAX_SECONDS = 25;
}
假设我可以有一个Constants类来包装常量,并将它们声明为静态final。我几乎不知道Java,我想知道这是否是创建常量的最佳方式。
当前回答
我更喜欢使用getter而不是常量。这些getter可能返回常量值,例如public int getMaxConnections() {return 10;},但任何需要常量的东西都将通过getter。
一个好处是,如果您的程序超出了常量的使用范围——您发现它需要可配置——您可以更改getter返回常量的方式。
另一个好处是,为了修改常量,你不必重新编译所有使用它的东西。当引用静态final字段时,该常量的值将被编译到引用它的任何字节码中。
其他回答
有一定的观点来回答这个问题。首先,java中的常量通常被声明为public、static和final。原因如下:
public, so that they are accessible from everywhere
static, so that they can be accessed without any instance. Since they are constants it
makes little sense to duplicate them for every object.
final, since they should not be allowed to change
我永远不会为CONSTANTS访问器/对象使用接口,因为接口通常是需要实现的。这看起来是不是很有趣:
String myConstant = IMyInterface.CONSTANTX;
相反,我会在一些不同的方法中进行选择,基于一些小的权衡,所以这取决于你需要什么:
1. Use a regular enum with a default/private constructor. Most people would define
constants this way, IMHO.
- drawback: cannot effectively Javadoc each constant member
- advantage: var members are implicitly public, static, and final
- advantage: type-safe
- provides "a limited constructor" in a special way that only takes args which match
predefined 'public static final' keys, thus limiting what you can pass to the
constructor
2. Use a altered enum WITHOUT a constructor, having all variables defined with
prefixed 'public static final' .
- looks funny just having a floating semi-colon in the code
- advantage: you can JavaDoc each variable with an explanation
- drawback: you still have to put explicit 'public static final' before each variable
- drawback: not type-safe
- no 'limited constructor'
3. Use a Class with a private constructor:
- advantage: you can JavaDoc each variable with an explanation
- drawback: you have to put explicit 'public static final' before each variable
- you have the option of having a constructor to create an instance
of the class if you want to provide additional functions related
to your constants
(or just keep the constructor private)
- drawback: not type-safe
4. Using interface:
- advantage: you can JavaDoc each variable with an explanation
- advantage: var members are implicitly 'public static final'
- you are able to define default interface methods if you want to provide additional
functions related to your constants (only if you implement the interface)
- drawback: not type-safe
更进一步,您可以将全局使用的常量放在接口中,这样它们就可以在系统范围内使用。如。
public interface MyGlobalConstants {
public static final int TIMEOUT_IN_SECS = 25;
}
但是不要执行它。只需在代码中通过完全限定类名直接引用它们。
一个好的面向对象设计不应该需要很多公开可用的常量。大多数常量都应该封装在需要它们完成工作的类中。
我不会称这个类与常量相同(除了大小写)…我将至少有一类“设置”,或“值”,或“常量”,所有的常量都将存在。如果我有很多,我会将它们分组在逻辑常量类中(UserSettings, AppSettings等)。
我同意使用界面不是正确的方法。在Bloch的《Effective Java》中,避免这种模式甚至有自己的项目(#18)。
Bloch反对常量接口模式的一个论点是,常量的使用是一个实现细节,但是实现一个使用它们的接口会在导出的API中暴露该实现细节。
public|private static final TYPE NAME = VALUE;模式是一种声明常数的好方法。就我个人而言,我认为最好避免创建一个单独的类来存放所有的常量,但除了个人偏好和风格之外,我还没有看到不这样做的理由。
如果常量可以很好地建模为枚举,请考虑1.5或更高版本中可用的枚举结构。
如果您使用的是1.5以前的版本,您仍然可以通过使用普通的Java类来实现类型安全枚举。(详见这个网站)。