The simplest, clearest, and least error prone solution is also the fastest (or fastest) that is often the case in Java.
Before you worry about speed, you should ask yourself; What is the easiest, clearest and least error prone approach? Passing values through static values is very error prone, for example when you have a multi-threaded application. Do not do this. Speed doesn't matter if your program is down.
However, in this case, the static fields are also misleading. It is not clear to the user what they should install first. If you use recursion, it will make your work even harder.
Even worse, local variables can be optimized more than static fields. This means that static fields are also slower, possibly much slower.
Note: to prevent JIT optimizations, JMH uses a black hole for the resulting value to stop code optimization, using the static field for this.
Option 3 is perhaps best if the value never changes, for example, if you have multiple values, however, if the value changes, it will be slower, because you add overhead to create the Test object each time, the JIT may not be optimized .
source share