Using reflection allows you to get knowledge about the classes loaded into a system in a highly dynamic way, the main benefit about using it would be that you can interact with classes that were not present at compile time. This is the good part. For example if you are building a Java to XML tool,
you should necessary use reflection to get to know the classes you want to map ( by the time you build the tool, those classes might not even exists ).
But this power comes at a price:
Quoting Joshua Bloch
1:
You lose all the benefits of all compile-time checking.
The code required to perform reflective access is clumsy and verbose.
Performance suffers.
Reflection is used only at design time. As a rule, objects should not be accessed reflectively in normal applications at run time.
and continues:
Sophisticated applications, like class browsers, object inspectors, code analysis tools, and interpretive embedded systems demand the use of reflection.[...] If you have any doubts as to whether your application falls into one of these categories, it probably doesn't
But what to do if you need to interact with classes that does not exists by the time you create your code? the best way is to provide an interface that the new classes must implement. An example is the
JDBC API. All java.sql.Connection, java.sql.PreparedStatement, java.sql.ResultSet and many others are interfaces. The database provider, implements these classes in its driver, thus allowing the programmer to access the database in a consistent way.
1 Joshua Bloch. Effective Java Programming Language Guide.
This is an excellent book.
[ February 24, 2006: Message edited by: Zkr Ryz ]