Skip to content

Instantly share code, notes, and snippets.

@mbernson
Last active December 25, 2015 02:58
Show Gist options
  • Save mbernson/6905981 to your computer and use it in GitHub Desktop.
Save mbernson/6905981 to your computer and use it in GitHub Desktop.
Java static property weirdness
public class Bar extends Foo {
public static String name = "Bar!";
}
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;
}
}
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