Is there any use for declaring objects as static?
Not really, no.
Of course, there is no performance benefit. This is because statics are actually stored in (hidden) static frame objects that live on the heap. Ultimately, JIT will generate native code to retrieve a static variable that is no faster than fetching an object variable.
It can be argued that it is more convenient to use statics to share some data structure βgloballyβ within an application. But this convenience has some significant drawbacks. The biggest of them is that statics makes testing harder and they make code reuse more difficult.
However, you should not confuse the general case with the specific case where the static value has or refers to an immutable value or data structure; for example, as a String constant or a constant mapping. They can be justified both from the point of view of design and from a practical point of view; eg.
public static final String THE_ANSWER = "Forty two"; private static final Map<String, Integer> SCORES; static { Map<String, Integer> map = new HashMap<>(); tmp.put("perfect", 100); tmp.put("average", 50); tmp.put("fail", 20); SCORE = Collections.unmodifiableMap(map); }
This is good ... so far there is no way for different (current or future) precedents to require different meanings / comparisons / whatever. If possible, then static can be harmful.
source share