java 在整个应用程序中,您在哪里使用常量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6220726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:55:19  来源:igfitidea点击:

Where do you keep Constants used throughout your application?

javainterfacecoding-style

提问by James Raitsev

Is interface an acceptable place to store my

接口是一个可以接受的地方来存储我的

public static final Foo bar

Do you extrapolate them to be read from outside of the program? Do you make up a super class for it?

您是否推断它们是从程序外部读取的?你为它补了一个超级班吗?

How do you do it, when situation presents itself?

当情况出现时,你是怎么做的?

回答by Michael Borgwardt

I'd put each constant into the class or interface it's most closely related to (e.g. because it will be use by its methods).

我会将每个常量放入与其最密切相关的类或接口中(例如,因为它将被其方法使用)。

A very seductive but ultimately very foolish idea is to have one "constants class" (or interface) that contains all constants used in the application. This looks "neat" at first glance, but is not good for maintainability because you want to group things by the functionality they implement, not by technical details like being constants (would you put all interfaces into a dedicated package? All abstract classes?).

一个非常诱人但最终非常愚蠢的想法是拥有一个包含应用程序中使用的所有常量的“常量类”(或接口)。乍一看,这看起来很“整洁”,但不利于可维护性,因为您想通过它们实现的功能来分组事物,而不是像常量这样的技术细节(您是否会将所有接口放入一个专用包中?所有抽象类?) .

The idea is also foolish because any change to that class/interface then (because of constant inlining) requires all classes that use any of the constants to be rebuilt - i.e. pretty much the entire app. So the bigger the app gets, the more frequently you need such a full rebuild, and the longer it takes. I worked on such a project, where this issue led to a 15 minute pause about every other day for every developer...

这个想法也很愚蠢,因为对该类/接口的任何更改(由于常量内联)都需要重建使用任何常量的所有类 - 即几乎整个应用程序。因此,应用程序越大,您需要进行这种完全重建的频率就越高,所需的时间也就越长。我参与了这样一个项目,这个问题导致每个开发人员每隔一天暂停 15 分钟......

回答by Abdullah Jibaly

If you are talking about a simple application the Constantsclass approach is fine:

如果你在谈论一个简单的应用程序,Constants类方法很好:

public class Constants {
    private Constants() {} // no way to instantiate this class
    public static final String MY_VAL = "123";
}

If you're building a larger app you should be using dependency injection, take a look at How can I inject a property value into a Spring Bean which was configured using annotations?

如果您正在构建一个更大的应用程序,您应该使用依赖注入,请查看如何将属性值注入使用注释配置的 Spring Bean?

回答by Will

I've used Abdullah Jibaly's approach but using nested classes, which provides a nice way for grouping consts. I've seen this go 3 levels deep and if good names are chosen, it can still work quite well.

我使用了 Abdullah Jibaly 的方法,但使用了嵌套类,它提供了一种对常量进行分组的好方法。我已经看到这深入 3 层,如果选择了好名字,它仍然可以很好地工作。

One might choose to use final classes for this instead of interface classes to avoid violating Item #19 on Josh Bloch's list (Effective Java 2nd edition).

人们可能会选择为此使用最终类而不是接口类,以避免违反 Josh Bloch 列表(Effective Java 2nd edition)上的第 19 项。

public final class Const {
    private Const() {} // prevent instantiation

    /** Group1 constants pertain to blah blah blah... */
    public static final class Group1 {
        private Group1() {} // prevent instantiation

        public static final int MY_VAL_1 = 1;
        public static final int MY_VAL_2 = 42;
    }

    /** Group2 constants pertain to blah blah blah... */
    public static final class Group2 {
        private Group2() {} // prevent instantiation

        public static final String MY_ALPHA_VAL = "A";
        public static final String MY_ALPHA_VAL = "B";
    }

}

回答by Dennis

This is bad practice. Classes are intended to be independent from one another therefore you should avoid global variables at all costs. A more realistic approach is to have a config file, usually in JSON, YAML, or XMLfile format, and have your program read from this file on start up.

这是不好的做法。类旨在相互独立,因此您应该不惜一切代价避免全局变量。更现实的做法是有一个配置文件,通常JSONYAMLXML文件格式,并具有从该文件中读取你的程序在启动时。

回答by Lirm

I would use an abstract final classsince it would be a more explicit declaration of the modifiers as opposed to using an interface. But, like Michael said, they should be logically grouped and be part of an existing class or interface if they semantically belong there.

我会使用 anabstract final class因为它是修饰符的更明确的声明,而不是使用接口。但是,正如迈克尔所说,如果它们在语义上属于现有类或接口,则它们应该在逻辑上分组并成为现有类或接口的一部分。