Last active
August 29, 2015 13:58
-
-
Save tavianator/9957738 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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