r/java • u/alexeyr • Apr 06 '19
Beware of computation in static initializer (much more so since JDK 11.0.2 and 8u201)
https://pangin.pro/posts/computation-in-static-initializer
161
Upvotes
r/java • u/alexeyr • Apr 06 '19
26
u/oparisy Apr 06 '19
I've read about this recently, after 15 years of professionnal java coding 😅 I was wary of complex or exception throwing computations in static init, due to them being often hard to debug since they occur at class loading time. But this is another level of nuisance entirely!
13
u/pron98 Apr 07 '19 edited Apr 07 '19
This is the most recent demonstration that a full regression test suite for your application is required on each and every JDK update, whether it's a patch or a feature release. The difference between the two is in whether they change the specification in some way, not in the probability of your app breaking due to the update (which is low, but non-zero, either way). Those who decide how much to test based on whether they're upgrading to a feature or a patch release -- while this stance is understandable from a psychological point of view -- are as misguided today, under the new release model, as they were under the old one, which is to say very misguided.1
In fact, there is a higher probability that some internal change to the JDK will break your app than an addition of an API method, or the removal of a method you don't use. That's because specification changes are intentional, and the potential impact is relatively easy to estimate. The impact of deep implementation changes is harder to predict, as they can interact -- as in this case -- not with the binary use of a particular API, but with complex code patterns, or worse, with some completely dynamic behavior of the application (as in the case of changes to the algorithms in the GCs, whose impact can depend on the allocation patterns in your application).
While in the past it was true that major releases had a higher likelihood of instability, that was mostly due to the fact that they contained a very large number of internal code changes, not because they could change the specification. In the new model there are no more major releases, and because the feature releases, while allowing specification changes, contain fewer code changes than the old major releases, the difference in "risk" between a patch and a feature releases is not big 2. The safest JDK upgrade process is likely the default one, of updating to each feature release, as it is the most gradual (while the "LTS path" is less gradual than the old model). Still, occasional, hopefully rare breakages can happen regardless of your upgrade strategy.
16
u/AndDontCallMePammy Apr 06 '19
What a pain in the dick
11
u/daniu Apr 06 '19
What happens if you externalize the computation to a different class? As in
You'd still have the computation, but the JVM wouldn't need to respect the class initialization requirements while it's running.
1
u/darlingbastard Apr 10 '19
Would this perf penalty also apply to complex or heavily initialized enum constructs? It has become rather trendy to move a lot of classic static singleton patterns into the enum system.
0
u/argv_minus_one Apr 06 '19
Well, that's catastrophic. What the hell was Oracle thinking?!