Should a java class' final fields always be static?

后端 未结 8 962
你的背包
你的背包 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: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.

提交回复
热议问题