Java CRTP: Works for container but not for methods?

Posted by Daniel on Stack Overflow See other posts from Stack Overflow or by Daniel
Published on 2010-06-08T02:40:46Z Indexed on 2010/06/08 2:42 UTC
Read the original article Hit count: 245

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about crtp