• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Generics and Reflection  RSS feed

 
Andrzej Zahorski
Greenhorn
Posts: 11
IntelliJ IDE Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy folks,
I don't understand in this example how the numbers are being taken,  extacly how the program knows it is an Integer if it's using String class constructor?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new Pair<String, Integer>("A", 1)
Because the generics state that the second parameter will be an Integer, the 1 will then be auto-boxed into an Integer type. An Integer does not have a copy constructor so type.getConstructor(type) throws an exception. The catch block looks to see if the super class is Number, and if so uses the constructor which takes a String, which all Number subclasses provide.
 
Andrzej Zahorski
Greenhorn
Posts: 11
IntelliJ IDE Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, for clear explanation
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!