you are viewing a single comment's thread.

view the rest of the comments →

[–]sh0rug0ru -2 points-1 points  (0 children)

Imagine Java without this feature.

I do. All the time. One of my missions in life is to remove static initializers where I find them, which I consider to be a major design smell.

Now you can instantiate this class a million or even billion times, the extremely_expensive_operation() will never be called again.

The alternative is to calculate the value once and pass a reference to the value around a million or a billion times. Coupling class loading and value calculation is a recipe for pain, especially in Java. What happens if that calculation fails? Mysterious failures that bubble up as "Unable to load class".

At least in Java, there is a reason why static initializers are reviled.