• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generics/newInstance of Class<T>

 
Tapio Niemela
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey, I was reading excellent Document about generics written by Brian Goetz. Still I don't seem to get the point about creating objects via reflection / Class

Goetz states
Consider a method that retrieves a set of objects from a database and returns a
collection of JavaBeans objects. You instantiate and initialize the created objects via
reflection, but that doesn't mean type safety has to go totally out the window.
Consider this method:


You can call this method simply like this:



I understand the idea, that one could have generic method to query database and receive JavaBeans of declared type. However I don't understand how this can be achieved. Goetz states that reflection is used to set to fields from result. Let's say we have database table for Cars and also corresponding JavaBean Car. Shouldn't therefore the method above be aware of which Class (Bean) it should construct (Car) and what setter methods to invoke (like setModel(String model) etc)? Or is Goetz trying to imply that reflection should be used to call correct setter methods based on instance of row? Because other solution would be modify the method signature following

However that way we would lose generic usage of the method

I'm very puzzled so any help greatly appreciated
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading your post I don't think the document you read was excellent because it assumed knowledge of reflection and left you puzzled.

Reflection is a special part of java. It allows you to "see" inside of classes/objects and "see" it's variables names and values, annotations, invoke methods and more. In your case you know the class so you can instantiate a new object and set it values from the values you obtained from the database. An example of this is JPA. It's a complex concept and is certainly not beginning java. Reflection should be avoided if possible because it's slower then regular java and you lose a lot of compile time checks. It can cause bugs that are very hard to spot.
 
Tapio Niemela
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:After reading your post I don't think the document you read was excellent because it assumed knowledge of reflection and left you puzzled.

Reflection is a special part of java. It allows you to "see" inside of classes/objects and "see" it's variables names and values, annotations, invoke methods and more. In your case you know the class so you can instantiate a new object and set it values from the values you obtained from the database. An example of this is JPA. It's a complex concept and is certainly not beginning java. Reflection should be avoided if possible because it's slower then regular java and you lose a lot of compile time checks. It can cause bugs that are very hard to spot.


Thanks for your reply. After I posted my "issue" I noticed that Goetz is really pointing to reflection. In his example the beans are assumed to be constructed via help of reflection (// use reflection to set fields from result). I agree that JPA is complex concept, but still I think the Document (https://www6.software.ibm.com/developerworks/education/j-generics/j-generics-a4.pdf) is pretty clear about generics.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving thread because the question is too difficult for "beginning".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic