This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Reflection Design Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reflection Design Question" Watch "Reflection Design Question" New topic
Author

Reflection Design Question

Albert Chern
Greenhorn

Joined: Feb 22, 2007
Posts: 2
Hi guys,

I have a program that can run with different sources of input as long as I give it a class that implements my Input interface. For example, I might have classes called TextInput or DatabaseInput that feed my program data.

I need to specify which input type to use on the command line, and what I do now is pass the class name as an argument and use reflection to construct an instance. People have told me that reflection is bad, however. Is there a better way to accomplish what I need?

Thanks!
Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
You will need reflection to create desired objects at runtime. In my project I have a similar situation were I create service objects configured in XML.
Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
You will need reflection to create desired objects at runtime. In my project I have a similar situation were I create service objects configured in XML.
Reflection has problems with performance. So the cache the object and use the interface methods to avoid any lookup.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Did those people also tell you why reflection is bad? Just because someone said it is, is not a good reason...

What you are describing sounds like a perfectly reasonable case for using reflection, so I wouldn't worry about it.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Reflection has problems with performance

This is a bit of an urban myth these days. JVMs handle reflection well now.



People have told me that reflection is bad

The main reason I hesitate to use it is because it removes compile time checking. So you shift the point where you discover a programming error from the compiler to run time, and as a result (without a good, comprehensive set of unit tests) things get missed. It also makes refactoring difficult.

That aside though, your example sounds like a good candidate for reflection.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Albert Chern
Greenhorn

Joined: Feb 22, 2007
Posts: 2
Thanks guys. Reflection also seemed to be the best choice to me, but I just wanted to confirm it with some experts.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Reflection Design Question
 
Similar Threads
what is java reflection
Java Reflection Problem
Creating an object of a random subclass
Dynamically generated objects
Using Reflection