0

How do I declare a list with the class name in a string. For eg: I have a className variable with the name of a class. I need to create List with the type in the className variable.

String className ="com.foo.Foo";

Is it possible to have a list that is having the same result of

List<Foo> fooList = new ArrayList<Foo>();

without knowing the type at the time of declaration.

4
  • Multipart question: How do I declare a list with the class name in a string: You could look into the class loader, else this is not the way to do this. Is it possible to have a list that is having the same result: Yes, it is: Just use List fooList = new ArrayList(); and cast the results when accessing the list (it is now just a list of (unchecked) Objects) Commented Nov 13, 2015 at 5:49
  • It sounds wrong. I may suppose that there is a method using reflection, but how will you use this list? How will you work in your code with something of unknown type? Commented Nov 13, 2015 at 5:50
  • Java uses Type Erasure. At execution time, the generic types are not present so... why don't create a List<Object> (which in most cases is, what the compiler make out of List<Something> anyway thanks to Type Erasure)? Commented Nov 13, 2015 at 5:50
  • @NorbertvanNobelen The second part as an example given to illustrate my question. I guess I should have been more clearer. In my case what I wanted, is to declared a list with what is there in the class name variable which is known only at run time. Commented Nov 13, 2015 at 7:07

3 Answers 3

2

It's not entirely clear, but it sounds like you're talking about making a list whose generic type parameter is determined at runtime, e.g. it could be a List<Foo> during one run and a List<Bar> during another.

You can't do that; type-checking, including generics, is done at compile time. It's the same reason you can't have a variable whose type is String during one run and Integer during another.

If you want to be able to choose different types at runtime, the generic type parameter has to be a supertype of all the types you might choose. For example, you could have a List<Object>, and put Foo instances in it during one run, and Bar instances during another.

Sign up to request clarification or add additional context in comments.

2 Comments

The type-checking at compile time is not the reason you cannot determine generics at runtime (C++ does type-checkin at compile time and there your can determine generics at runtime). The actual reason is Type Erasure.
It's not specific to generics: the type of every variable, generic or not, must be known at compile time. That's true in C++ as well. (And note that C++ doesn't actually have generics; it has templates, which use similar syntax but work very differently.) You can have a List<?> variable that can refer to either a List<Foo> or List<Bar>, chosen at runtime, but you can't add any elements to it because the compiler doesn't know what type of objects it requires.
0

Multipart question "How do I declare a list with the class name in a string": you can not declare like in that way. for "Is it possible to have a list that is having the same result of": you can create something like "List fooList = new ArrayList();" But you should be take care of the type casting while accessing the fooList.

1 Comment

The second part as an example given to illustrate my question. I guess I should have been more clearer. In my case what I wanted, is to declared a list with what is there in the class name variable which is known only at run time.
0

Jackson has this issue and solves it like this

com.fasterxml.jackson.core.type.TypeReference<List<String>> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.