Is it possible to access a child class constant from within a static method in the parent class?
public class Model {
public static void someMethod(){
HERE I WANT TO GET THE MODEL_NAME constant!
}
}
public class EventModel extends Model {
public static final String MODEL_NAME = "events";
}
and in some other place I call:
EventModel.someMethod();
Try it!
If the constant is declared
private, then no. If it ispublic, then yes, as anyone can access it. The parent class is largely irrelevent here.Foois defined inParent, and thus cannot access private members ofChild.How about this?
Note however, that the method is no longer public.