在Java中实现常量的最佳方法是什么? [关闭]

本文翻译自:What is the best way to implement constants in Java? [closed]

I've seen examples like this: 我见过这样的例子:

public class MaxSeconds {
   public static final int MAX_SECONDS = 25;
}

and supposed that I could have a Constants class to wrap constants in, declaring them static final. 并且假设我可以有一个Constants类来包装常量,声明它们是静态final。 I know practically no Java at all and am wondering if this is the best way to create constants. 我几乎不知道Java,我想知道这是否是创建常量的最佳方法。


#1楼

参考:https://stackoom.com/question/HBa/在Java中实现常量的最佳方法是什么-关闭


#2楼

static final是我的偏好,我只用一个enum ,如果该项目确实是枚举。


#3楼

I use static final to declare constants and go with the ALL_CAPS naming notation. 我使用static final声明常量并使用ALL_CAPS命名表示法。 I have seen quite a few real life instances where all constants are bunched together into an interface. 我已经看到了很多真实生活实例,其中所有常量都聚集在一起形成一个接口。 A few posts have rightly called that a bad practice, primarily because that's not what an interface is for. 一些帖子正确地称这是一种不好的做法,主要是因为这不是一个界面的用途。 An interface should enforce a contract and should not be a place to put unrelated constants in. Putting it together into a class that cannot be instantiated (through a private constructor) too is fine if the constant semantics don't belong to a specific class(es). 接口应该强制执行一个契约,并且不应该放置不相关的常量。如果常量语义不属于特定的类,那么将它放在一个无法实例化的类中(通过私有构造函数)也是可以的。 ES)。 I always put a constant in the class that it's most related to, because that makes sense and is also easily maintainable. 我总是在课堂上放一个与它最相关的常数,因为这是有道理的,也很容易维护。

Enums are a good choice to represent a range of values, but if you are storing standalone constants with an emphasis on the absolute value (eg. TIMEOUT = 100 ms) you can just go for the static final approach. 枚举是表示一系列值的不错选择,但如果要存储独立常量并强调绝对值(例如,TIMEOUT = 100 ms),您可以选择static final方法。


#4楼

I agree with what most are saying, it is best to use enums when dealing with a collection of constants. 我同意大多数人所说的,最好在处理常量集合时使用枚举。 However, if you are programming in Android there is a better solution: IntDef Annotation . 但是,如果您在Android中编程,则有一个更好的解决方案: IntDef Annotation

@Retention(SOURCE)
@IntDef({NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST,NAVIGATION_MODE_TABS})
public @interface NavigationMode {}
public static final int NAVIGATION_MODE_STANDARD = 0;
public static final int NAVIGATION_MODE_LIST = 1;
public static final int NAVIGATION_MODE_TABS = 2;
...
public abstract void setNavigationMode(@NavigationMode int mode);
@NavigationMode
public abstract int getNavigationMode();

IntDef annotation is superior to enums in one simple way, it takes significantly less space as it is simply a compile-time marker. IntDef注释以一种简单的方式优于枚举,它占用的空间显着减少,因为它只是一个编译时标记。 It is not a class, nor does it have the automatic string-conversion property. 它不是一个类,也没有自动字符串转换属性。


#5楼

There is a certain amount of opinion to answer this. 有一定的意见来回答这个问题。 To start with, constants in java are generally declared to be public, static and final. 首先,java中的常量通常被声明为public,static和final。 Below are the reasons: 以下是原因:

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

I would never use an interface for a CONSTANTS accessor/object simply because interfaces are generally expected to be implemented. 我永远不会为CONSTANTS访问器/对象使用接口,因为通常希望实现接口。 Wouldn't this look funny: 这看起来不好笑:

String myConstant = IMyInterface.CONSTANTX;

Instead I would choose between a few different ways, based on some small trade-offs, and so it depends on what you need: 相反,我会根据一些小的权衡取舍,在几种不同的方式之间做出选择,这取决于你需要什么:

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

#6楼

What is the difference 有什么不同

1. 1。

public interface MyGlobalConstants {
    public static final int TIMEOUT_IN_SECS = 25;
}

2. 2。

public class MyGlobalConstants {
    private MyGlobalConstants () {} // Prevents instantiation
    public static final int TIMEOUT_IN_SECS = 25;
}

and using MyGlobalConstants.TIMEOUT_IN_SECS wherever we need this constant. 并在我们需要此常量的任何地方使用MyGlobalConstants.TIMEOUT_IN_SECS I think both are same. 我认为两者都是一样的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值