Should a java class' final fields always be static?

后端 未结 8 893
你的背包
你的背包 2021-01-03 22:36

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

相关标签:
8条回答
  • 2021-01-03 22:39

    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.

    0 讨论(0)
  • 2021-01-03 22:43

    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.

    0 讨论(0)
  • 2021-01-03 22:49

    Absolutely not. Immutable objects, for example, have final properties, that can be set only once, by the constructor.

    For more information, please see: 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.

    0 讨论(0)
  • 2021-01-03 22:52

    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

    0 讨论(0)
  • 2021-01-03 22:54

    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.

    0 讨论(0)
  • 2021-01-03 22:55

    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

    0 讨论(0)
提交回复
热议问题