Should a java class' final fields always be static?

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

提交回复
热议问题