Reflection in and of itself is slow in nature. See this question for more details. There are several reasons for this. John Skeet perfectly explains this:
Check for a constructor without parameters. Check the availability of the constructor without parameters. Make sure the caller has access to use reflection at all. Work out (at run time) how much space you need to assign a call to the constructor code (because it will not know in advance that the constructor is empty)
Basically, reflection should perform all of the above steps before calling, while calling a normal method should do much less.
The JIT code for instantiating B is incredibly lightweight. Basically, it should allocate enough memory (this is just increasing the pointer if GC is not required), and what about that - there is no constructor code to actually call; I don’t know if JIT skips this or not, but in any case it’s not so much.
With that said, there are many cases where Java is not dynamic enough to do what you want, and reflection provides a simple and clean alternative. Consider the following scenario:
- You have a large number of classes that represent various elements, i.e.
Car , Boat and House . - They both extend / implement the same class:
LifeItem . - The user enters one of three lines: "Car", "Boat" or "House".
- Your goal is to access the LifeItem method based on the parameter.
- The first approach that comes to mind is to build an if / else structure and build the desired
LifeItem . However, it is not very scalable and can become very dirty if you have dozens of LifeItem implementations.
Reflection can help here: it can be used to dynamically build a LifeItem object based on the name, so the input "Car" will be sent to the Car constructor. Suddenly, there could be hundreds of lines of if / else code turning into a simple reflection line. The latter scenario will not be as important on the Java 7+ platform due to the introduction of switch statements with strings, but even then a switch with hundreds of cases is something I would like to avoid. Here what difference between purity will look in most cases:
Without reflection:
public static void main(String[] args) { String input = args[0]; if(input.equals("Car")) doSomething(new Car(args[1])); else if(input.equals("Boat")) doSomething(new Boat(args[1])); else if (input.equals("House")) doSomething(new House(args[1])); ...
Considering reflection, it can turn into:
public static void main(String[] args) { String input = args[0]; try { doSomething((LifeItem)Class.forName(input).getConstructor(String.class).newInstance(args[1])); } catch (Exception ie) { System.err.println("Invalid input: " + input); } }
Personally, I would say that the latter is more accurate, more concise and more convenient than the first. In the end, this is a personal preference, but this is only one of many cases where reflection is useful.
In addition, when using reflection, you should try to cache as much information as possible. In other words, use simple, logical things, such as not calling the get(Declared)Method everywhere, if you can help it: rather, store it in a variable so that you don't have the overhead of re-linking to a link when you want it use.
So these are the two extremes of pro and con reflection. To summarize, if reflection improves the readability of the code (as in the presented scenario), it will certainly go for it. And if you do, just think about reducing the number of calls to the get* : these are the simplest settings.