Today I'm running into the following issue:
I have three classes, (superclass) A, (subclass) B and my Main class. Here are some code examples of that structure:
public abstract class A {
public static String testValue;
public static boolean valueSet() {
return testValue != null;
}
}
public class B extends A {
static {
System.out.println("TEST");
testValue = "foo";
}
}
public class Main {
public static void main(String[] args) {
System.out.println(B.testValue);
System.out.println(B.valueSet());
System.out.println(new B());
System.out.println(B.testValue);
System.out.println(B.valueSet());
}
}
I'd expect the following output:
foo
true
foo
true
however, the code outputs this instead:
null
false
foo
true
My final goal is to set testValue
to some value in every subclass, and implement the checking function just once in the superclass. It has to be static, because it should just check for compatibility of a String against an Object of type A
. Definining the using a method as public static abstract
is not possible, as I've already read, and there is no way to abstract values, so I can't use that as well. I want the code to stay as simple as possible, without reimplementing the check for every subclass.
Thanks for any help or tips!