Skip to content

Instantly share code, notes, and snippets.

@MihailJP
Created December 7, 2012 21:37
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 MihailJP/4236740 to your computer and use it in GitHub Desktop.
Save MihailJP/4236740 to your computer and use it in GitHub Desktop.
You cannot override a class (static) constant (Java)
class Parent {
static final int Val = 1;
public int getval() {
return Val;
}
}
class Child extends Parent {
static final int Val = 2;
}
public class ConstTest1 {
public static void main(String[] args) {
Child instance = new Child();
System.out.println(instance.getval()); // You'll see '1', expected '2'.
}
}
class Parent {
int Val() {return 1;}
public int getval() {
return Val();
}
}
class Child extends Parent {
int Val() {return 2;}
}
public class ConstTest2 {
public static void main(String[] args) {
Child instance = new Child();
System.out.println(instance.getval());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment