• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Instantiating variable with a type parameter using Strings

 
Greenhorn
Posts: 2
Mac C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys! I was wondering if it's possible to create an object of a parameterized type using String names for the classes. For example, create an object of type ArrayList<Double> using "ArrayList" and "Double". Is this possible using Class, ParameterizedType, or some other classes? Thank you.
 
Sheriff
Posts: 13510
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search for compiling Java code at runtime
 
Junilu Lacar
Sheriff
Posts: 13510
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Marshal
Posts: 24586
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear in mind that the type parameters don't exist at runtime, they only exist so that the compiler can enforce type safety. For example whereas your code might say ArrayList<Double>, at compile time it's only an ArrayList.

That makes most of your problem go away. And yes, you can use a Class object to create a Constructor, from which you can call newInstance to create a new instance of the class.
 
Suds Panda
Greenhorn
Posts: 2
Mac C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the warm welcome guys! I will look into compiling code at runtime further, since I'm trying to do something like generating Java stubs and creating jars on the fly to load in dynamically.

Paul Clapham wrote:Bear in mind that the type parameters don't exist at runtime, they only exist so that the compiler can enforce type safety. For example whereas your code might say ArrayList<Double>, at compile time it's only an ArrayList.

That makes most of your problem go away. And yes, you can use a Class object to create a Constructor, from which you can call newInstance to create a new instance of the class.



Thanks for your help! I was reviewing my application logic and realized I wouldn't really need a type parameter so I can simply use newInstance to make an object instead.

But that's a good point about type parameters. It just always confused me since I wasn't sure how Java can simply forget them at runtime. Like if I did want to make an ArrayList<Double> on the fly during runtime, how could it prevent other types of data from being added to it?
 
Paul Clapham
Marshal
Posts: 24586
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Suds Panda wrote:Like if I did want to make an ArrayList<Double> on the fly during runtime, how could it prevent other types of data from being added to it?



It can't prevent that. It relies on the compiler having rejected code which tries to add other types of data.

Of course you could create an ArrayList using reflection and then cast it to ArrayList<Double>. But that would be rather pointless so I suppose you have some more general scheme where you're using reflection to create instances of classes?
 
Paul Clapham
Marshal
Posts: 24586
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Suds Panda wrote:I was reviewing my application logic and realized I wouldn't really need a type parameter so I can simply use newInstance to make an object instead.



Just FYI... the Class.newInstance() method is deprecated in Java 9, that's why I suggested the long way around with a Constructor.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!