public abstract class SuperClass {
public int x, y;
public static int z;
}
I want every subclass of SuperClass to have the static variable z. Naturally z will contain a different value for each subclass. I'm hoping to avoid defining z in every subclass, since it's going to be a functional dependancy of values x and y; Is this possible?
Unlike instance variables that are "one per instance",
staticvariables are not "one per subclass" - they are "one per declaring class". In other words, subclasses ofSuperClassshareSuperClass.z, but they cannot "override" it on a class-by-class basis.It does not mean that you cannot implement it yourself: on way to make your own per-subclass storage of integers is adding a
static Map<Class,int> zstoSuperClass, with optional functions for accessing the data: