Should a java class' final fields always be st

2019-02-11 18:59发布

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.

8条回答
Animai°情兽
2楼-- · 2019-02-11 19:02

They don't always come together and it's not a convention. final fields are often used to create immutable types:

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.

1 - well, class loader-wide, to be precise

查看更多
放我归山
3楼-- · 2019-02-11 19:07

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:

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.

查看更多
欢心
4楼-- · 2019-02-11 19:14

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

查看更多
淡お忘
5楼-- · 2019-02-11 19:15

No, absolutely not - and it's not a convention.

static and final are entirely different things. static means that the field relates to the type rather 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 fields are usually for constants - whereas instance fields which are final are usually used when creating immutable types.

查看更多
Lonely孤独者°
6楼-- · 2019-02-11 19:19

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
}
查看更多
【Aperson】
7楼-- · 2019-02-11 19:21

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.

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.

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.

查看更多
登录 后发表回答