I want to set the name and the code static attributes in the milk class.
Why doesn’t this work?
I can’t even override the setters/getters of the name and the code.
public abstract class Cow {
private static String name;
private static String code;
...
public static String getName() {
return name;
}
public static void setName(String name) {
Cow.name = name;
}
public static String getCode() {
return code;
}
public static void setCode(String code) {
Cow.code = code;
}
...
}
public class Milk extends Cow{
...
static {
Milk.setName("asdmilk");
Milk.setCode("KDDFA");
}
..
}
You don’t get one set of static variables per subclass of
Cow, and you can’t override static methods at all. Static members aren’t polymorphic, basically.It’s not clear why you’re trying to do this, but basically if you declare a static variable, there’s one variable, full stop. Calling
Milk.setNameis exactly equivalent to callingCow.setNamehere.