Java CRTP: Works for container but not for methods?
- by Daniel
I have a baseclass with a protected static ArrayList. I want to have a seperate ArrayList for each kind of subclass that extends this baseclass. This is when I applied CRTP:
public class BaseExample<T> {
protected static ArrayList<Integer> data = new ArrayList<Integer>();
}
This works just fine.
However, when I try to implement the following static method in the same base class, it doesn't adhere to CRTP:
public static void clear() {
data.clear();
}
For example:
class SubExample extends BaseExample<SubExample> {
// insertion methods accessing 'data' field
// these work fine :)
}
SubExample.clear(); // does not seem to clear data container
Do I need to somehow explicitly specify T in my baseclass clear method?
Note: These are all pure static classes.