0

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!

4
  • Don't initalise static final in the constructor... if you need it different per constructor make it a public final. but not static... Now, if you want the same data everywhere, the values you defined in the sample above are populated the moment your application starts, and will never be redefined. Commented Jun 16, 2016 at 10:12
  • 2
    You are safe to to assume that the initialization will happen exactly once and all objects will see the exact same instance. However, there's a separate issue with your code: SimpleDateFormat is not thread-safe. So you should never keep an instance in a constant like this. Commented Jun 16, 2016 at 10:13
  • @MichaelDibbets / @Ray : So to get the global and same value everywhere for each instance, we can create a static final field and it will be used across all future instances correctly? Right? And what happens if one changes this static value (if it wasn't final) inside an instance (just asking for curiosity sake) Commented Jun 16, 2016 at 10:20
  • static changes are global. everywhere. a static variable can be accessed via the class, but isnot really part of theclass. Its a global. Commented Jun 16, 2016 at 10:27

1 Answer 1

3

A class is only initialised once, and in a thread-safe manner.

There are strong guarantees about this in the Java Language Specification.

The implementation of the Java Virtual Machine is responsible for taking care of synchronization and recursive initialization by using the following procedure.

So having final static fields is fine, you can't go wrong with it. So much so that it is sometimes exploited to provide thread-safe singletons, via the static holder pattern. The field will be initialised exactly once and everyone will see the same value for the field*. For practical purposes you can assume the field was always there and will be there forever.

On the other hand, writing data to static fields from instance methods (and that includes constructors) is usually a big no-no.

*There are a few, obscure scenarios when this is not true, but you don't need to worry about them, unless you have circular dependencies between static initialisers, which you shouldn't have anyway.

Sign up to request clarification or add additional context in comments.

2 Comments

Just for curiosity's sake, what happens when a class updates its static variable from an instance method? Do future creations see this updated value? Or just the presently created instances see this value?
@SudhanshuShekhar A static field is just like a global variable really. It isn't tied to instances of the class at all. And therein lies the problem: in a multi-threaded environment some threads may see the new value, others may see the old value. And when two threads try to update the same static field at the same time (without proper synchronization), even stranger things can happen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.