如何提高JAVA反射机制创建对象的效率


   
  Class clazz = Class.forName("Demo");
  
Object demo1 = clazz.getConstructor().newInstance();
   
  Demo demo2 = new Demo();
 

创建demo1要比创建demo2慢很多,
有什么办法可以提高java反射机制创建对象的效率?

java

大概是帽子 11 years, 6 months ago

Oracle的官方文档里已经说了,反射API会慢一些。如果确实是性能要求比较高的应用,最好不要用发射。

http://docs.oracle.com/javase/tutorial/reflect/index.html

Performance Overhead
Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.

你是真皮虾发 answered 11 years, 6 months ago

Your Answer