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.
She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
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 ]
posted 14 years ago
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.
posted 14 years ago
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.
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.)