In Java, unlike C++, this is not possible due to the way that generics are implemented. In Java, there is only one class for each generic type, not multiple copies for each time a different type argument is used (this is called erasure). As a result, you can't have a single variable that points to the class object of its subtype, because there can be many subtypes but there is always exactly one copy of the static field. This contrasts with C++, where each time the ILogger
template is instantiated you would get your own copy of that static field.
One possible approximation would be to have a Map
as a static field that associates class objects with strings, as in
public static final Map<Class, String> classMap = new HashMap<Class, String>();
You would then have to have each subtype explicitly add itself to this map, perhaps with a static initializer:
public class Sub implements ILogger<Sub> {
static {
ILogger.classMap.put(Sub.class, /* ... value ... */);
}
public Sub() {
Log.debug(LogTag, ...);
}
}
Hope this helps!