'public static final' or 'private stat

2019-01-30 06:26发布

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 FOO = 5;

Would be equivalent in result to this:

private static final int FOO = 5;
...
public static getFoo() { return FOO; }

But which is better practice?

7条回答
Anthone
2楼-- · 2019-01-30 07:11

The first one if the getFoo result is costant and not need to be evaluated at runtime.

查看更多
登录 后发表回答