I have a model that holds a public final static variable which I would like to use to instantiate a private final static from another class (a Spring @service
class), e.g.
public class MyClass {
public static final String MY_CLASS_MSG = "My Class";
}
@Service
public class MyService {
private static final String MY_SERVICE_MSG = "My Service" + MyClass.MY_CLASS_MSG;
}
Can I always guarantee that MY_SERVICE_MSG
always gets properly instantiated?
Yes. In the general case, classes are loaded lazily and the first thing that happens when doing so is running the static initializers/evaluating static attributes, which will itself trigger the loading of any class which contains attributes that these static initializers or attributes refer to.
As ruakh@ pointed out, the compiler has special rules when the definition of the constant is purely static (i.e. doesn't involve any method call). In this case, the compiler itself can evaluate the constants, which is sometimes necessary (for example if this value is reference in an annotation's field, which has to be processed statically).
In any case, there should never be a problem with what you're doing to my knowledge. What was your concern?