Skip to content

Nested generic types in static method

I was looking for a solution to this problem, I need to build the method so that it returns someObject<anotherObject>, I was thinking about building something like this:

public static <T, K> T<K> jsonToObjectType(String json, TypeReference<T<K>> type) {
    // More code

But it doesn’t compile, is there any way to perform the method? Thank you!


public static <T, K> T<K>

Your T has no bounds, meaning, T can be anything. It could be String.

String has no generics, so how can T<K> make sense? It doesn’t, hence, java doesn’t let you compile this.

I guess you could conceive of the notion of: “T is some specific type, could be anything, as long as it has exactly 1 generics param”, but java doesn’t have this, and never will, because that is structural type and java doesn’t do that.

However, note that a generics param can be any type, notably include types that are themselves parameterized. Here is a trival example:

public static <T> T coalesce(T a, T b) {
    return a == null ? b : a;

This method can be used like so:

String a = null;
String b = "Hello";

coalesce(a, b).toLowerCase();

There is absolutely no problem feeding it 2 List<String>, at which point the expression coalesce(listA, listB) would be of type List<String>. And that’s just with <T>, not with this <T, K> stuff.

I don’t quite know what jsonToObjectType is supposed to do, but assuming that it is supposed to take a string that contains JSON + some super-type-token (you can search the web for that term), which I’m 99.9% certain you have, then just remove K from it all, and you get precisely what you wanted:

public static <T> T jsonToObjectType(String json, TypeReference<T> type) {
   // code here 

and you can call it like so:

String json = "["Hello", "World!"]";
List<String> list = jsonToObjectType(json, new TypeReference<List<String>>() {});

and it’ll compile without warnings or errors and works.