Designing constructors around type erasure in Java

Posted by Internet Friend on Stack Overflow See other posts from Stack Overflow or by Internet Friend
Published on 2010-04-24T07:14:14Z Indexed on 2010/04/24 7:23 UTC
Read the original article Hit count: 248

Filed under:
|
|
|

Yesterday, I was designing a Java class which I wanted to be initalized with Lists of various generic types:

TheClass(List<String> list) {
   ...
}

TheClass(List<OtherType> list) {
   ...
}

This will not compile, as the constructors have the same erasure.

I just went with factory methods differentiated by their names instead:

public static TheClass createWithStrings(List<String> list)
public static TheClass createWithOtherTypes(List<OtherType> list)

This is less than optimal, as there isn't a single obvious location where all the different options for creating instances are available.

I tried to search for better design ideas, but found surprisingly few results. What other patterns exist for designing around this problem?

© Stack Overflow or respective owner

Related posts about java

Related posts about type-erasure