Skip to content

Instantly share code, notes, and snippets.

@tavianator
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tavianator/9957738 to your computer and use it in GitHub Desktop.
Save tavianator/9957738 to your computer and use it in GitHub Desktop.
class FinalFieldExample {
final int x;
int y;
static FinalFieldExample f;
public FinalFieldExample() {
x = 3;
y = 4;
}
static void writer() {
f = new FinalFieldExample();
}
static void reader() {
if (f != null) {
int i = f.x; // guaranteed to see 3
int j = f.y; // could see 0
}
}
}
class FinalFieldExample {
static HasFinalField f;
static void writer() {
f = new HasFinalField();
}
static void reader() {
if (f != null) {
int i = f.x; // guaranteed to see 3
int j = f.y; // could see 0
}
}
}
class HasFinalField {
final int x;
int y;
public HasFinalField() {
x = 3;
y = 4;
}
}
class FinalFieldExample {
HasFinalField f;
void writer() {
f = new HasFinalField();
}
void reader() {
if (f != null) {
int i = f.x; // guaranteed to see 3
int j = f.y; // could see 0
}
}
}
class HasFinalField {
final int x;
int y;
public HasFinalField() {
x = 3;
y = 4;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment