By Maxouille

2018-10-11 14:18:02 8 Comments

I'm using generic methods in Java and I would like to use as argument a List of custom object.

My main class is this:

public class Main {

    public static <T> T executeGetRequest(String target, Class<T> resultClass) {

        //MY STUFF
        T result = resultClass.newInstance();
        return result;

    public static void main(String[] args) {
        executeGetRequest("myList", List<myCustomObject>.class); // ERROR HERE

I would like to use as argument a List<myCustomeObject>. When I'm using List.class, there is no error but I'm not sure that the result will be cast in a myCustomObject.


@Federico Peralta Schaffner 2018-10-11 15:13:13

Don't use a Class<T> argument, along with reflection (i.e. Class.newInstance()). Use a Supplier<T> instead:

public static <T> T executeGetRequest(String target, Supplier<T> factory) {

    // MY STUFF

    T result = factory.get();
    return result;

Then, invoke it as follows:

List<myCustomObject> result = executeGetRequest("myList", () -> new ArrayList<>());

You can even use the diamond operator (<>) when creating the ArrayList, as this is infered from the left hand side by the compiler (i.e. List<myCustomObject>).

You can also use a method reference:

List<myCustomObject> result = executeGetRequest("myList", ArrayList::new);

@frant.hartm 2018-10-11 14:38:50

If you always return a list of items, then use List<T> as a return type:

public class Main {

    public static <T> List<T> executeGetRequest(String target, Class<T> resultClass) throws IllegalAccessException, InstantiationException {

        T item = resultClass.newInstance();
        List<T> result = new ArrayList<>();

        return result;

    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        executeGetRequest("myList", Foo.class);

    static class Foo {


@Vadim 2018-10-11 14:51:06

Code is very broken...

  1. List<myCustomObject>.class is wrong it can be only List.class

  2. List is an Interface and call of List.class.newInstance(); will throw an exception anyway

  3. Even if you will do code like:

    List<myCustomClass> myList = new ArrayList(); Object myResult = executeGetRequest("myList", myList.getClass());

You will get back myResult as instance of ArrayList class...

You need to reconsider what do you try to achieve - get back a List of myCustomClass objects or new instance of myCustomClass

BTW: at Runtime there is a "Type erasure" and there is no way to get what object types are in the List from List implementation.

In short at Runtime it is always List<Object>

@Eugene 2018-10-11 15:35:34

is no way to get what object types are in the List from List implementation, of course there is, use TypeTokens

@Vadim 2018-10-11 19:33:37

@Eugene TypeToken from where? Google Gson? There is no such thing as TypeToken in JDK at least up to Java 1.8. I did not check higher. Still there is Type erasure: System.out.println(((Type)new ArrayList<String>().getClass().getTypeParameters()[0].getBou‌​nds()[0])); prints class java.lang.Object

@Eugene 2018-10-11 19:37:15

I was not saying in JDK, it's in guava, but of course it relies on jdk code, just that guava makes it a lot easier to use

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] Sort ArrayList of custom Objects by property

63 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 3696215 View
  • 3122 Score
  • 63 Answer
  • Tags:   java random integer

20 Answered Questions

[SOLVED] Create Generic method constraining T to an Enum

35 Answered Questions

29 Answered Questions

[SOLVED] How to create a generic array in Java?

19 Answered Questions

[SOLVED] How to Sort a List<T> by a property in the object

16 Answered Questions

13 Answered Questions

[SOLVED] Google Gson - deserialize list<class> object? (generic type)

7 Answered Questions

[SOLVED] How do I use reflection to call a generic method?

3 Answered Questions

[SOLVED] Collections.emptyList() returns a List<Object>?

Sponsored Content