简体   繁体   English

'public static final' 还是 'private static final' with getter?

[英]'public static final' or 'private static final' with getter?

In Java, it's taught that variables should be kept private to enable better encapsulation, but what about static constants?在 Java 中,教导变量应该保持私有以实现更好的封装,但是 static 常量呢? 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?但哪种做法更好?

There's one reason to not use a constant directly in your code.不在代码中直接使用常量是有原因的。

Assume FOO may change later on (but still stay constant), say to public static final int FOO = 10;假设 FOO 以后可能会改变(但仍然保持不变),对public static final int FOO = 10; . . Shouldn't break anything as long as nobody's stupid enough to hardcode the value directly right?只要没有人愚蠢到直接对值进行硬编码,就不应该破坏任何东西吗?

No. The Java compiler will inline constants such as Foo above into the calling code, ie someFunc(FooClass.FOO);不会。 Java 编译器会将上述 Foo 等常量内联到调用代码中,即someFunc(FooClass.FOO); becomes someFunc(5);变成someFunc(5); . . Now if you recompile your library but not the calling code you can end up in surprising situations.现在,如果您重新编译您的库而不是调用代码,您可能会遇到令人惊讶的情况。 That's avoided if you use a function - the JIT will still optimize it just fine, so no real performance hit there.如果您使用 function 就可以避免这种情况——JIT 仍会对其进行优化,因此不会影响真正的性能。

Since a final variable cannot be changed later if you gonna use it as a global constant just make it public no getter needed.由于 final 变量以后不能更改,如果您要将它用作全局常量,只需将其公开即可,不需要 getter。

Getter is pointless here and most likely will be inlined by the JVM. Just stick with public constant. Getter 在这里毫无意义,很可能会被 JVM 内联。只需坚持公共常量。

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.对于常量,它没有多大意义。

Use the variales outside the class as:使用 class 之外的变量作为:

public def FOO:Integer = 5; 

If you encapsulation is not your priority.如果封装不是您的首要任务。 Otherwise use the second variant so that you expose a method and not the variable.否则使用第二个变体,以便公开方法而不是变量。

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

Is also a better practice for code maintenance to not rely on variables.代码维护不依赖变量也是一种更好的做法。 Remember that "premature optimization is the root of all evil".请记住“过早的优化是万恶之源”。

The first one if the getFoo result is costant and not need to be evaluated at runtime.第一个如果 getFoo 结果是恒定的并且不需要在运行时评估。

I'd stay with the getFoo() since it allows you to change the implementation in the future without changing the client code.我会继续使用 getFoo(),因为它允许您在将来更改实现而无需更改客户端代码。 As @Tomasz noted, the JVM will probably inline your current implementation, so you pay much of a performance penalty.正如@Tomasz 指出的那样,JVM 可能会内联您当前的实现,因此您付出了很大的性能损失。

The advantage of using setter and getter on member is to be able to overwrite.在成员上使用 setter 和 getter 的优点是能够覆盖。 This is not valid for static "methods" (rather functions)这对 static “方法”(而不是功能)无效

There also no way to define interfaces static methods.也没有办法定义接口 static 方法。

I would go with the field access我会 go 与现场访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM