aspose file tools*
The moose likes Java in General and the fly likes generics/newInstance of Class<T> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "generics/newInstance of Class<T>" Watch "generics/newInstance of Class<T>" New topic
Author

generics/newInstance of Class<T>

Tapio Niemela
Ranch Hand

Joined: Jan 06, 2006
Posts: 77
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

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Tapio Niemela
Ranch Hand

Joined: Jan 06, 2006
Posts: 77
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

Joined: Oct 13, 2005
Posts: 39415
    
  28
Moving thread because the question is too difficult for "beginning".
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: generics/newInstance of Class<T>