Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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