'public static final' or 'private static final' with getter?

前端 未结 7 2024
花落未央
花落未央 2020-12-23 00:05

In Java, it\'s taught that variables should be kept private to enable better encapsulation, but what about static constants? This:

public static final int FO         


        
7条回答
  •  情话喂你
    2020-12-23 00:13

    Getter is pointless here and most likely will be inlined by the JVM. Just stick with public constant.

    The idea behind encapsulation is to protect unwanted changes of a variable and hide the internal representation. With constants it doesn't make much sense.

提交回复
热议问题