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

JavaStaticPrivatePublicFinal

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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;. 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, i.e. someFunc(FooClass.FOO); becomes 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.

Solution 2 - Java

Since a final variable cannot be changed later if you gonna use it as a global constant just make it public no getter needed.

Solution 3 - Java

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.

Solution 4 - Java

Use the variales outside the class as:

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".

Solution 5 - Java

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

Solution 6 - Java

I'd stay with the getFoo() since it allows you to change the implementation in the future without changing the client code. As @Tomasz noted, the JVM will probably inline your current implementation, so you pay much of a performance penalty.

Solution 7 - Java

The advantage of using setter and getter on member is to be able to overwrite. This is not valid for static "methods" (rather functions)

There also no way to define interfaces static methods.

I would go with the field access

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris CumminsView Question on Stackoverflow
Solution 1 - JavaVooView Answer on Stackoverflow
Solution 2 - JavaSerdar DogruyolView Answer on Stackoverflow
Solution 3 - JavaTomasz NurkiewiczView Answer on Stackoverflow
Solution 4 - JavaprofimedicaView Answer on Stackoverflow
Solution 5 - Javadash1eView Answer on Stackoverflow
Solution 6 - JavashamsView Answer on Stackoverflow
Solution 7 - Javastefan bachertView Answer on Stackoverflow