简体   繁体   English

java类的最终字段是否应该是静态的?

[英]Should a java class' final fields always be static?

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. 但只是想知道一个类中的最终字段是否应该始终是static还是只是一个约定。 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. 基于我对它们的用途的理解,我觉得做的事情比语言强加的更合乎逻辑。

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
}

No, absolutely not - and it's not a convention. 不,绝对不 - 而且这不是一个惯例。

static and final are entirely different things. staticfinal完全不同的东西。 static means that the field relates to the type rather than any particular instance of the type. static表示该字段与类型相关,而不是与该类型的任何特定实例相关。 final means that the field can't change value after initial assignment (which must occur during type/instance initialization). final意味着字段在初始赋值后不能更改值(必须在类型/实例初始化期间发生)。

static final fields are usually for constants - whereas instance fields which are final are usually used when creating immutable types. static final字段通常用于常量 - 而final 实例字段通常在创建不可变类型时使用。

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字段并不常见且非常棘手。 static final is seen often because it means application 1 -wide constant. 经常看到static final因为它意味着应用程序1- wide常量。

1 - well, class loader-wide, to be precise 1 - 井,类装载机范围,确切地说

Final fields do not need to 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: 标记为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字段仍然会被标记为private ,但是,这更多的是额外的检查,因此编译器可以确保您再也不会设置该属性。

If you want to access them like ClassName.FIELD , then yes, you have to do that. 如果你想像ClassName.FIELD那样访问它们,那么是的,你必须这样做。 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. 如果你不使它成为静态的,你必须做一些类似new ClassName().FIELD ,这是不必要的,并且是一个无意义的对象创建。

However, if you are only using it in the class or making it private , then don't make it static. 但是,如果您只在课堂上使用它或将其private ,那么请不要将其设为静态。 If you are within the actual class, you can just do FIELD . 如果你在实际的课程中,你可以做FIELD

To fully grasp this concept, you have to know what static means. 要完全掌握这个概念,你必须知道static意味着什么。 Static means that it belongs to the actual class, not an instance of it. 静态意味着它属于实际的类,而不是它的实例。

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. 放弃final ,班级变得可变。 Add a static , and all your points are the same, and there is no legal way to write the constructor. 添加static ,所有点都相同,没有合法的方法来编写构造函数。

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属性的唯一情况,但它们提供了它们有用性的明显示例。

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 ......这意味着这是一个常数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM