I have this class declaration
abstract class A {
protected static String message = "Hello";
}
And I want this field to get propagated to subclasses, to avoid typing protected static String message = "Whatever"; (the value of this static field is different in each subclass) in all the subclasses.
I know this won’t work:
class B extends A {
static {
message = "Blablabla";
}
}
Because it will actually modify A‘s message because static fields/methods are not inherited. What I want to know is if there’s any other way of doing this, or if I have to rewrite the static field on each subclass.
There’s no straightforward way to do it.
You may consider use some utility class which maps a Class object to a message. This will help you use general methods to work with messages based on
this.getClass().