0

In my team we had a small discussion if a field declared 'private static final' in a class has any advantages from just declaring it 'private final'. For example if I have the following line in my class:

private static final String a = "a String";

Is this really better than just declaring it like this:

private final String a = "a String";

If it would be a variable that can be used outside of this class, declaring it static makes total sense. But as it is private I see no real advantage apart from a static variable being only created once and is than referenced in all objects. We have lots of tests and those define variables like this. Those tests are only run once when the system starts and are no longer needed after that. Apart from that is there anything I am missing?

12
  • 2
    A static field would not be re-initialized every time you create an instance of said type. Commented Jul 8, 2019 at 8:59
  • 1
    I see no reason to ever declare something like final without static if the contents always stays the same. You're unnecessarily creating new objects. For a String this overhead is minimal because the actual text is interned, but if you ever create more complex objects in production code and 'forget' to make them static, you can get performance issues that you may not even be aware of. It's better to make it a habit to make them static. Commented Jul 8, 2019 at 9:01
  • Also declaring static will not affect the memory imprint/serialization and whatnot of said type Commented Jul 8, 2019 at 9:02
  • 1
    One other difference is that static variables can be accessed by both static and non-static methods. Instance variables can not be accessed by static methods. Commented Jul 8, 2019 at 9:03
  • Look from the usage angle. Only static variables can be accessed in any static methods you might have. If there was no static methods- then its basically a constant which is singleton. Commented Jul 8, 2019 at 9:03

1 Answer 1

-1

In short:

You use private final when you only need to set the value once, but the value needs to be different for each instance of that class.

You use private static final when all instances of your class need to have access to one unchanging value. It is useful for avoiding magic strings / numbers and enhancing readability, particularly if your value is a longer string. If you start defining too many of these, consider moving them to a configuration file.

So static in combination with final and private is certainly useful, but staticin general requires careful, deliberate usage. Don't just slap it on everything; instead, your default action should be to analyze whether or not you need it, and implement accordingly.

Sign up to request clarification or add additional context in comments.

2 Comments

"your default action should be to keep your fields on a per-instance level.". I disagree. Your default action should be to analyze your requirements and what your goal is and based on that choose your approach.
Fair enough, I'll edit to include that then.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.