java:使用最终静态 int = 1 是否比使用普通 1 更好?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2070669/
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-29 19:15:33  来源:igfitidea点击:

java: is using a final static int = 1 better than just a normal 1?

java

提问by Omu

I just saw a class (an big API module), where there is alot of stuff like

我刚看到一个类(一个大的 API 模块),里面有很多东西,比如

readParamString(Object params, int index)

and they have in that class 10 fields

他们在那个班级有 10 个字段

final static int PARAM1 = 1
...
final static int PARAM10 = 10

which are being used with readParam functions

与 readParam 函数一起使用

but you can also use a normal int like readParamString(o, 1);

但你也可以使用普通的 int 像 readParamString(o, 1);

is final static int somehow better than using a normal int ?

final static int 是否比使用普通 int 更好?

采纳答案by giri

By declaring with the keywords final static int, you will have a single instance of a variable that has the integer value of 1. An important point is that this value cannot change once declared with the keyword final. Otherwise, you can change the value of variable. You use the keywords final staticif you don't want to change the variable value anywhere your code.

通过使用关键字声明final static int,您将拥有整数值为 1 的变量的单个实例。重要的一点是,一旦使用关键字声明,该值就无法更改final。否则,您可以更改变量的值。final static如果您不想在代码的任何地方更改变量值,则可以使用关键字。

回答by Adamski

One advantage of declaring a static final constant is to prevent multiple references to the same literal value througout the code, which could potentially lead to unwanted variation; e.g. if a programmer inadvertently changes one literal without changing the others.

声明静态最终常量的一个优点是防止在整个代码中多次引用相同的文字值,这可能会导致不必要的变化;例如,如果程序员无意中更改了一个文字而没有更改其他文字。

In your example, I'd question the value of declaring a constant PARAM1 with the value 1, especially if this is private to the containing class and only referenced once. If however, there was some semantic meaning to the literal value then it might be more useful; e.g.

在你的例子中,我会质疑声明一个值为 1 的常量 PARAM1 的价值,特别是如果它是包含类的私有并且只被引用一次。然而,如果字面值有一些语义意义,那么它可能更有用;例如

public static final int LOAD_ACTION = 1;
public static final int SAVE_ACTION = 2;
public static final int DELETE_ACTION = 4;
public static final int MOVE_ACTION = 8;

However, typically an enum would be used instead of this approach.

但是,通常会使用枚举而不是这种方法。

回答by Thorbj?rn Ravn Andersen

That's silly.

那是愚蠢的。

The reason for extracting numeric constants is to give them a telling name. PARAM1..PARAM10 are not very telling.

提取数字常量的原因是给它们一个有说服力的名字。PARAM1..PARAM10 不是很有说服力。

回答by Michael Burr

In the example you give, I'd say that having the named values is not better than just using the int directly. The parameter is a count, so the int representation of the parameter is just as descriptive as the named version.

在您给出的示例中,我会说拥有命名值并不比直接使用 int 好。参数是一个计数,因此参数的 int 表示与命名版本一样具有描述性。

This seems to be kind of like when I occasionally come across C code that has things like

这似乎有点像我偶尔遇到的 C 代码有这样的东西

#define ZERO 0
#define ONE 1

There's really not much point. In fact, when I see those names used (instead of when I'm looking at the definitions), I get tempted to jump to the defines to make sure the names aren't lying (It's been a long, long time, but, I've actually come across code that had FALSE defined to 1, -1 or something non-zero, for example, which is really, really bad).

真的没有太大的意义。事实上,当我看到使用这些名称时(而不是查看定义时),我很想跳到定义以确保名称没有说谎(已经很长很长时间了,但是,我实际上遇到过将 FALSE 定义为 1、-1 或非零值的代码,例如,这真的非常糟糕)。



Edit: on second look, it appears that the names might indicate which field in the first parameter that are of interest, in which case the names might make more sense - as long as the names have some meaningful tie to the field rather than just the numeric offset. It's hard to say whether or not that's the case, since you might be paraphrasing the code you're really working with.

编辑:再看,似乎名称可能表明第一个参数中感兴趣的字段,在这种情况下,名称可能更有意义 - 只要名称与字段有一些有意义的联系,而不仅仅是数字偏移。很难说情况是否如此,因为您可能正在解释您真正使用的代码。

回答by DerMike

I think it's not done for the final static variable but to have telling names. And therefor even PARAMETER1is more telling than just 1. Therefor may improve understanding while reading the code.

我认为这不是为最终的静态变量完成的,而是有明确的名称。因此,甚至PARAMETER1比仅仅更能说明问题1。因此可以在阅读代码时提高理解。

回答by Upul Bandara

yes somehow better than using a normal int

是的,比使用普通的 int 更好

But If you are using Java 5 or later enumis better than final staticvariables.

但是如果你使用的是 Java 5 或更高版本enum则比final static变量更好。

回答by nanda

Maybe it's because the variable is inlined by the compiler.

可能是因为该变量是由编译器内联的。