Java 类的最终字段是否应该始终是静态的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14913442/
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
Should a java class' final fields always be static?
提问by Karthick S
I could not find any references online about this. But just wanted to know if final fields in a class should always be static
or is it just a convention. Based on my understanding of their uses, I feel that it is more of a logical thing to do than something that is imposed by the language.
我在网上找不到任何关于此的参考资料。但只是想知道一个类中的 final 字段是否应该始终是static
或者它只是一个约定。根据我对它们用途的理解,我觉得这更符合逻辑,而不是语言强加的事情。
回答by JB Nizet
Of course not. They must be static if they belong to the class, and not be static if they belong to the instance of the class:
当然不是。如果它们属于类,它们必须是静态的,如果它们属于类的实例,它们必须是静态的:
public class ImmutablePerson {
private static final int MAX_LAST_NAME_LENGTH = 255; // belongs to the type
private final String firstName; // belongs to the instance
private final String lastName; // belongs to the instance
public ImmutablePerson(String firstName, String lastName) {
if (lastName.length() > MAX_LAST_NAME_LENGTH) {
throw new IllegalArgumentException("last name too large");
}
this.firstName = firstName;
this.lastName = lastName;
}
// getters omitted for brevity
}
回答by Jon Skeet
No, absolutely not - and it's nota convention.
不,绝对不是——而且这不是约定俗成的。
static
and final
are entirelydifferent things. static
means that the field relates to the typerather than any particular instance of the type. final
means that the field can't change value after initial assignment (which must occur during type/instance initialization).
static
并且final
是完全不同的东西。static
意味着该字段与类型相关,而不是与类型的任何特定实例相关。final
意味着该字段在初始分配后不能更改值(必须在类型/实例初始化期间发生)。
static final
fields are usually for constants - whereas instancefields which are final
are usually used when creating immutable types.
static final
字段通常用于常量 - 而实例字段final
通常在创建不可变类型时使用。
回答by Tomasz Nurkiewicz
They don't always come together and it's not a convention. final
fields are often used to create immutable types:
他们并不总是走到一起,这也不是惯例。final
字段通常用于创建不可变类型:
class Person {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
On the other hand static
but not final
fields are not that common and are quite tricky. static final
is seen often because it means application1-wide constant.
另一方面,static
但不是final
领域并不常见,而且非常棘手。static final
经常看到,因为它意味着应用程序1范围的常量。
1 - well, class loader-wide, to be precise
1 - 准确地说,是类加载器范围内的
回答by Alexis King
Final fields do not needto be static, and sometimes it can be useful to have a non-static final instance variable. Fields that are marked both static and final are usually used for constants, like this:
Final 字段不需要是静态的,有时拥有一个非静态的 final 实例变量会很有用。标记为 static 和 final 的字段通常用于常量,如下所示:
public static final int BORDER_WIDTH = 5;
However, sometimes you'll see a non-static final field when an object has a immutable property. Usually, non-static final
fields are still marked private
for the usual reasons, though, so it's more of an extra check so the compiler can make sure you're never setting the property again.
但是,当对象具有不可变属性时,有时您会看到非静态 final 字段。通常,出于通常的原因,final
仍然标记非静态字段private
,因此它更多是额外的检查,以便编译器可以确保您永远不会再次设置该属性。
回答by Doorknob
If you want to access them like ClassName.FIELD
, then yes, you have to do that. If you don't make it static, you have to do something like new ClassName().FIELD
, which is unnecessary and a pointless creation of an object.
如果您想像 那样访问它们ClassName.FIELD
,那么是的,您必须这样做。如果您不将其设为静态,则必须执行类似 的操作new ClassName().FIELD
,这是不必要的且毫无意义的创建对象。
However, if you are only using it in the class or making it private
, then don't make it static. If you are within the actual class, you can just do FIELD
.
但是,如果您只是在课堂上使用它或制作它private
,则不要将其设为静态。如果您在实际课程中,则可以执行FIELD
.
To fully grasp this concept, you have to know what static
means. Static means that it belongs to the actual class, not an instance of it.
要完全掌握这个概念,您必须知道是什么static
意思。静态意味着它属于实际的类,而不是它的实例。
回答by Eric
Absolutely not. Consider:
绝对不。考虑:
class Point {
public final int x;
public final int y;
public Point(int _x, int _y) {
x = _x;
y = _y;
}
}
Drop the final
, and the class becomes mutable. Add a static
, and all your points are the same, and there is no legal way to write the constructor.
删除final
,该类将变为可变的。加一个static
,你所有的点都一样,没有合法的写构造函数的方法。
回答by gd1
Absolutely not. Immutable objects, for example, have final
properties, that can be set only once, by the constructor.
绝对不。例如,不可变对象具有final
只能由构造函数设置一次的属性。
For more information, please see: http://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
更多信息请参见:http: //docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
Immutable objects are not the only case in which final
properties are used, but they provide a evident example of their usefulness.
不可变对象并不是final
使用属性的唯一情况,但它们提供了一个明显的例子来说明它们的用处。
回答by ERROR Source Uncompilable
The answer is no.
答案是不。
static
静止的
"Indicates that only one such data field is available for all instances of this class. Without this modifier, each instance has its own copy of a data field"
...meaning there can only be one of this
“表示只有一个这样的数据字段可用于此类的所有实例。没有此修饰符,每个实例都有自己的数据字段副本”
...意味着只能有一个
final
最后
"The value provided for the data field cannot be modified"
...meaning that this is a constant
“无法修改为数据字段提供的值”
...意味着这是一个常数