0

I have ended up in a situation, where I can use reflection to get a Constructor object and then using that Constructor to instantiate a class or I can use Constructor reference to instantiate it. I wanted to know which is better in terms of performance. I know accessing a method with reflection gives low performance than accessing it directly, but if I use Constructor reference then it should create a dynamic anonymous type to get the instance I wanted. Which is better Reflection or Method reference?

interface ConstructorRef {
      public abstract Component getInstance();
}

class Component1 implements Component {
}

class Main {
      public static void main(String[] args) {
           ConstructorRef reference = Component1::new;
           Component component = reference.getInstance();

           Class<? extends Component> classObj = Component1.class;
           Constructor<? extends Component> constructorObj = classObj.getConstructor();
           Component component1 = constructorObj.getInstance();
      }
}

Actually there is so many Components and I will only know which component to instantiate at runtime, The number of components will grow as new requirements come. So, I am restricted to use one of the above methods I can't use direct object instantiation using new operator. So, which is better among the two alternatives?

  • 1
    The non-reflective (native) way is always going to be better performing than the reflective way. See https://stackoverflow.com/questions/435553/java-reflection-performance – mario_sunny Nov 04 '19 at 13:36
  • I know, but as I said before I am restricted to use either reflection or method reference by coding needs. I am going to create number of constants which group constructors as a chunk. At run time I should be able to decide what are the objects I need based on the constant input. So I can’t use direct object instantiation. – Immanuel Francis Nov 05 '19 at 03:09
  • The constructor reference *is* a non-reflective way. So in most circumstances, it will be faster or on par with the reflective operation. Further, it provides additional safety regarding types and exceptions. – Holger Nov 05 '19 at 10:44
  • "method reference" like `Component1::new` its kinda just shortcut to `() -> new Component1()` so its just constructor call wrapped in another method – GotoFinal Nov 05 '19 at 11:21
  • I have tried to test the performance of instances creation using both Reflection and Constructor reference. But the results are confusing. When I try to instantiate 1000 objects using both methods one after another using a single thread, the reflection seems to perform better. But if I repeat the instance creation a number of times ( one loop body will create 1000 objects per a method by both methods ), now I get different result. The constructor ref method only lags at the first time from the second time it seems constructor reference is faster then reflection. ( see next comment) – Immanuel Francis Nov 14 '19 at 02:54
  • But if I create 1000 objects per method by both methods using one thread for one bunch creation and a another thread for the second bunch the reflection method is the obvious winner. Why java works like this? Is there any caching mechanism for anonymous types with in thread? You know, caches the anonymous type so it doesn’t need to create a new anonymous type with in the same thread flow. – Immanuel Francis Nov 14 '19 at 03:02
  • If you manage to write an application without ever using lambda expressions or method references, then the very first (only) use of a constructor reference will pay the initial overhead of loading the runtime support. You don’t see the initial overhead of Reflection, because it has been used to invoke your main method already. Likewise, when using Java modules, the module loader does already use lambda expression internally, so the initial overhead will disappear from your naive benchmark. See also [How do I write a correct micro-benchmark in Java?](https://stackoverflow.com/q/504103/2711488). – Holger Dec 10 '19 at 11:04

0 Answers0