0

I understand

  1. Static fields belongs/associates to CLASS type
  2. Its used by all Object of that class
  3. If the class is loaded by two different class loader in a same JVM then we can have two copies.

Is there any way/scenario that I can have two copies of my static with different values ?

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
sandejai
  • 931
  • 1
  • 15
  • 22
  • 3
    Yes, if you have two classloaders, as per point 3. – Jon Skeet Feb 10 '14 at 09:55
  • Also when multithreading a value change to a variable can be not visible to another thread immediately. So in a way the static field could simultaneous have 2 values (in the different cores caches) – Richard Tingle Feb 10 '14 at 09:59
  • It is not really two copies. Consider the case of two classes B and D, who both have a static field foo. Would we say we have two copies of foo? Certainly not, and the same situation is it when we have the "same" class loaded by two loaders: those two classes are treated as different as B and D are. – Ingo Feb 10 '14 at 10:07

2 Answers2

0

If your class C is loaded by two different class loaders CL1 and CL2,
then there are two instances c1, c2 of java.lang.Class describing your
class C. Then in each one of them (c1, c2) you can have different values
in your static field f (f is defined in C).

Some people even argue that technically these two c1, c2
are different classes as far as the JVM is concerned.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • This is indeed so, the two C classes are treated like two entirely unrelated classes by the JVM. For this reason, the situation is the same as with two classes, say, B and D, who happen to have a static field `foo`. – Ingo Feb 10 '14 at 10:04
  • Yes, I know. Actually c1 and c2 may be the class C (I mean compiled version of the source file) at different moments of time. So it is normal the two classes are different as the bytecode may be completely different two. – peter.petrov Feb 10 '14 at 10:06
  • Thanks a lot peter.petrov,Ingo,Richard Tingle . – sandejai Feb 10 '14 at 10:20
  • but if I have only one class loader then can I have two copies of my Static fields by any means ? – sandejai Feb 10 '14 at 10:20
  • Not really, only in the sense which Richard Tingle meant. But these are really two thread-cache copies of the same static variable. The value in the shared static field is just one (I am talking reference type static field here). – peter.petrov Feb 10 '14 at 13:20
  • But if you have static long field x, and 2 threads are setting values into it (v1 and v2 values), then at certain point of time it could happen that neither v1, nor v2 is in the static field but some mixture of both. That's because setting long fields is not atomic but setting reference fields is. – peter.petrov Feb 10 '14 at 13:21
0

Yes, you can have this case for static final primitives.

Let's consider the following case:

  1. Class A has public static final int SOME_CONSTANT = 1
  2. Class B references A.SOME_CONSTANT
  3. Class A and B are compiled
  4. Class B changes to public static final int SOME_CONSTANT = 2
  5. Class B is re-compiled WITHOUT recompiling class A

If you were to now start a JVM (with A and B on the classpath), the value of the static will be 1 in class A whereas class B will have a value of 2. This is because javac compiles a copy of the value into each .class file. Class B has it's own copy of the static and does not reference class A. To fix this, you MUST recompile class A every time the constant changes in class B (ie do a clean build instead of an incremental build). Note, this problem is only for static final primitives (int, long etc).

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • Well Thank you very much for this insight idea to stumble on the issue. But is this scenario ever gonna happen, i.e. One Class A already loaded and Blass B unloaded and reloaded and communicate in the same way ? Also its very much like loading a class again and similar to Loading 2 class using 2 Class loader. So What I understand that in normal circumstances two thread can not have hold two different values of a same STATIC field. – sandejai Feb 10 '14 at 14:51
  • Please note that this is NOT a runtime issue. This is a compile time issue. It has nothing to do with classloaders and is caused by the compiled .class files containing different values. – lance-java Feb 10 '14 at 14:59