Does creating an object using reflection rather than calling the class constructor result in any significant performance differences?
Does creating an object using reflection rather than calling the class constructor result in
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes – absolutely. Looking up a class via reflection is, by magnitude, more expensive.
Quoting Java’s documentation on reflection:
Here’s a simple test I hacked up in 5 minutes on my machine, running Sun JRE 6u10:
With these results:
Bear in mind the lookup and the instantiation are done together, and in some cases the lookup can be refactored away, but this is just a basic example.
Even if you just instantiate, you still get a performance hit:
Again, YMMV.