61

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?

1
  • 1
    Voting to close on not constructive (based on the opinionated feedback already offered). Is FOO really a constant? Is the class providing FOO part of an API? Or part of an end point application? There are mathematical constants which never change. There are also bit flags that should never change (see SWT). So the answer is "It depends."
    – Tim Bender
    Apr 6, 2012 at 18:57

7 Answers 7

76

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.

7
  • 5
    Interesting, I never realized this. I never come across constants with getters in the wild though. Apr 6, 2012 at 21:03
  • 4
    @Chris Depends on what your constants are - there are lots of constants that are set in stone (Pi for example is quite unlikely to turn out to be 3 after all these years ;) ) so I wouldn't condemn the use of public constants in all cases. But one should keep this in mind, because debugging such a problem is extremely horrible.
    – Voo
    Apr 7, 2012 at 17:21
  • 2
    Wow! +10! I've been in the business for longer than I care to mention, and I've always wondered why I shouldn't just use a public constant when it seems safe to do so! Nov 13, 2012 at 23:15
  • 1
    Excuse me, my English is not very good, I didn't see the correct conclusion. Someone told me what the correct answer should be?
    – 袁文涛
    Mar 6, 2019 at 3:03
  • 3
    @TJ If the "constant" could change in the future use a getter. If the constant will always be the same you can use a public const.
    – Voo
    Mar 6, 2019 at 9:31
10

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

1
  • 9
    Only if you're absolutely certain you'll never change the final variable though. constants are inlined by the compiler which can lead to extremely surprising results if they change later on and you don't recompile everything. Edit: Figured, that's worth an answer to make it a bit more prominently.
    – Voo
    Apr 6, 2012 at 18:51
7

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.

4

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

1

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

1

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.