• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to define the type of an Collection at run time  RSS feed

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to define the type of a collection at runtime.

For example,

if i have an Arraylist which has to be created based on the type, may be integer sometimes, or String or any object. I will know the type only at runtime.

I cannot declare the arraylist like ArrayList<String> arrList = new ArrayList<String>(), as this will not work for any other object type. I do not want to declare the arraylist raw as well.

Please suggest......
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't specify the generic type at run time.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. Search for "type erasure" to find out why.
 
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not exactly "run time", but this can help you:



My knowledge about generics is bad, someone please correct me if wrong...
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
means anything and everything is excepted by the list.....

You wouldn't like to do this your type safety is gone for a toss unless you have very good reason to do so
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhat Jha wrote: means anything and everything is excepted by the list.....

You wouldn't like to do this your type safety is gone for a toss unless you have very good reason to do so


ye, i agree with that. I do not know a situation where i do not know what my collection will hold.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot specify the type of a collection at runtime, and it is not useful to do so.

Java is a statically typed language. That means that the compiler needs to know all the types of the variables when you compile the program. If you don't know the type at compile time, you can't make use of Java's static type checking. If you know the type only at runtime, then that's too late for type checking by the compiler - because when your program is running, it has already been compiled.

So, there is no way to do this. It's like you want to eat the cake before you bake it.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhat Jha wrote: means anything and everything is excepted by the list.....

Actually, no. It means it can return anything and everything but you cannot add anything at all to the list. After all, the ? could in reality be String, Integer, Foo, ... The compiler can never know that what you add is allowed so it simply disallows everything.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!