• Post Reply Bookmark Topic Watch Topic
  • New Topic

using java reflection  RSS feed

 
gaurikay ke
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what are the pros and cons of using java reflection(java.lang.reflect.*)?
Thanks
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a homework question? Here's some good reading material on the topic from Bruce Eckel's Thinking in Java.
 
gaurikay ke
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.But no,this is not a homework question.I guess I phrased the question like that.I wanted to know in terms of performance.
 
Jaime M. Tovar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know reflection is a very powerful tool, it gives you a new level of abstraction to work with. In practice and in my own experience it is a little more difficult to debug applications based in reflection. Also if you don�t do a good design it will lead you to problems only visible at runtime. Reflection slows a little the application (in my opinion not a significant matter compared with the benefits), but it will help you a lot with scalability.

So if I where you I will use reflection to save me development time but use that time in a good design. Performance will no be my very primary issue with this decision.
 
uj johansson
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by gaurikay ke:
what are the pros and cons of using java reflection?

The cons are that you're by-passing the strong typing of Java. That may not seem very serious but it is, because type safety is one of the corner stones of java. Generics for example has been added for the sole purpose of increasing type safety. Gosling even labels reflection a sin (in the quote at the beginning of the reflection chapter of The Java Progreamming Language).

I let others talk about the cons. Only remember that if your needs of reflection mount up you should consider a language with slack typing instead of Java. There are several. Groove even uses the JVM and is compatible with Java code.
[ December 31, 2005: Message edited by: uj johansson ]
 
uj johansson
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jaime Tovar:
As far as I know reflection is a very powerful tool, it gives you a new level of abstraction to work with.


In fact reflection lowers the abstraction level. It's a sin as Gosling states.
 
gaurikay ke
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the inputs.The reason I was considering using reflection is for this requirement:In the jsp page,a table is to be generated dynamically with column names to be read from the database and then populate the rows with corresponding values.For Ex. if the Column header is Name then the value to be populated should be name; i.e getName() should be called on the bean.So I check what is column then invoke the method.

Thanks,
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And do you have an alternate way of doing it that doesn't use reflection? That would be the only reason to compare the two ways. Asking whether X is better than Y when you can't do Y doesn't get you very far.

(And no, I don't know of an alternate way. I used reflection in a similar situation when my data was coming from an LDAP directory.)
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!