0

Is there any performance gain from using final modifier on non-primitive static data in Java?

For example:

static final Thread t2 = new Thread(new Thread_2());

versus:

static Thread t2 = new Thread(new Thread_2());

I mean, static final for primitives defines a true constant and is good to be used, in the sense that it's value is known at compile time, but could the use of final trigger any optimizations in this non-primitive case?

Does using final in this case does something or it's a waste?

Not style-wise/good practice answers please, but performance-wise only.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
pdrak
  • 404
  • 1
  • 4
  • 12

2 Answers2

3

The JVM can make some optimizations if it knows a value will never change (for example, disabling null checks), so it will lead to some small performance gain. In almost all circumstances, though, this gain will be too small to notice or worry about. I don't know much about your program, but I would guess that time spent making variables final would be better spent on developing a more efficient algorithm.

nullptr
  • 2,244
  • 1
  • 15
  • 22
  • I am making a skeleton on which other code will be put. I just want to make sure this skeleton is as much performance-wise optimized as possible. It is just a theoretical question though. Putting final in every non-changing variable is not that much time-consuming. I am a worshiper of clean code though, and i would like to avoid using extra keywords that do nothing if possible, but at the same time i can not stand the possibility that it would be faster (even slightly) if i'd use 'em ;-) – pdrak May 09 '13 at 00:33
  • 2
    @PetrosDrakoulis I'm a perfectionist in very much the same way, but I had to learn to overcome it sometimes and look at the big picture. Still, if they really never change, it's probably a good idea to mark them `final` anyway – nullptr May 09 '13 at 00:35
  • I've been told by people who know more Java than I do that the compilers of today can figure out when a field isn't going to be changed, at least if it isn't public or perhaps protected. So you may not gain much in optimization at all, if the compiler already did it. If I had a global object out of necessity, I'd probably mark it final. – Andrew Lazarus May 09 '13 at 00:42
  • @AndrewLazarus Yes, i have heard the same thing. But what about the most common JIT (hotspot i think)? – pdrak May 09 '13 at 00:45
2

While there can be a small performance gain, and some static final values will be embedded in the class in which they are used, to my mind the biggest benefit is from the compiler enforcing the intent & design that the value does not change - that is, the gain is to the developer(s).

And in my way of thinking, skeleton/framework code should be the best it can be in every way, not just performance, but stylistically too.

I would encourage you to declare everything as final unless it has a real need to be mutable - that is, things are final by default, unless needed to be otherwise.

Lawrence Dol
  • 63,018
  • 25
  • 139
  • 189