I am using JCStress
to test for the final variable. I know that final
can be used to make sure that when you construct an object, another thread accessing that object doesn't see that object in a partially constructed state.
Now I have a class A.java as such
public class A {
final int f;
A() {
this.f = 42;
}
}
According to my knowledge, the constructor should be executed as such
A temp = <new>
temp.f = 42
<freeze value>
fv = temp
Now I'm using the below-mentioned test.
@JCStressTest
@State
public class FinalField {
A a;
@Actor
public void writer() {
a = new A();
}
@Actor
public void reader(I_Result result) {
A ta = a;
if (ta != null) {
result.r1 = ta.f;
}
}
}
Now, why is it that I see the value 0 in my output? My CPU architecture is x86, so reordering stores with loads also doesn't make sense. The output I get is
0 94,922,153 FORBIDDEN No default case provided, assume FORBIDDEN
42 48,638,587 ACCEPTABLE Final value initialized
Also, one more thing that I found unusual is that when I declare the field a
as static
. I get only 42 as my output, and why is that?
42 299,477,390 ACCEPTABLE Final value initialized