Last active
December 25, 2015 02:58
-
-
Save mbernson/6905981 to your computer and use it in GitHub Desktop.
Java static property weirdness
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
public class Bar extends Foo { | |
public static String name = "Bar!"; | |
} |
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
public class Foo { | |
protected static String name = "Foo!"; | |
public String getName() { | |
// This probably keeps pointing to `Foo.name`, So `Bar.name` never gets used. | |
// After copying the getter to the subclass, the problem goes away. :( | |
return name; | |
} | |
} |
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
public class Test { | |
public static void main(String[] args) { | |
System.out.println((new Foo()).getName()); | |
System.out.println((new Bar()).getName()); | |
/* | |
* Outputs: | |
* Foo! | |
* Foo! | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment