Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherit a Static Variable in Java

I want to have the following setup:

abstract class Parent {
    public static String ACONSTANT; // I'd use abstract here if it was allowed

    // Other stuff follows
}

class Child extends Parent {
    public static String ACONSTANT = "some value";

    // etc
}

Is this possible in java? How? I'd rather not use instance variables/methods if I can avoid it.

Thanks!

EDIT:

The constant is the name of a database table. Each child object is a mini ORM.

like image 864
SapphireSun Avatar asked Nov 30 '22 09:11

SapphireSun


1 Answers

you can't do it exactly as you want. Perhaps an acceptable compromise would be:

abstract class Parent {
    public abstract String getACONSTANT();
}

class Child extends Parent {
    public static final String ACONSTANT = "some value";
    public String getACONSTANT() { return ACONSTANT; }
}
like image 54
stew Avatar answered Dec 04 '22 14:12

stew