This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

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.
 
Marshal
Posts: 14018
233
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
Marshal
Posts: 14018
233
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!
 
Sheriff
Posts: 24654
58
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
Sheriff
Posts: 24654
58
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
Sheriff
Posts: 24654
58
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.
 
Normally trees don't drive trucks. Does this tiny ad have a license?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!