I have a class which stores several static final fields as shown:
class MyExampleClass{
public static final DateFormat T_FORMAT = new SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH);;
public static final logger LOGGER = new OwnLogger();
...
}
This code is run as a service inside jboss(multiple-threads). It was pointed out that the above code might throw initialization errors as all instances of this class will refer to the same memory location and will try to initialize it. However, since the variable is final they will be unable to do so throwing exceptions.
I tried searching around but couldn't find any reference that addresses this query. So I had the following questions:
1) Static fields are class level variables and thus will be the same for all instances of that class. But when we initialize them in the constructor, does this mean that JVM is over-writing the previous value with the new one each time a new instance is created?
2) Iff we are over-writing the field (as asked above), the final should throw an error right?
3) Assuming we are not over-writing: Once a class explicitly over-writes the static field content, all other classes see this same value. But how long does this new value last? Until all instances of the are killed and after some delay we instantiate a new one? Or forever?
Am quite confused. Would appreciate any help!
SimpleDateFormatis not thread-safe. So you should never keep an instance in a constant like this.static finalfield and it will be used across all future instances correctly? Right? And what happens if one changes this static value (if it wasn'tfinal) inside an instance (just asking for curiosity sake)