vendredi 14 décembre 2018

getDeclaredConstructor() not working on private constructor with no parameters

I have a class with a single method less private constructor that I am trying to use reflection on. The following illustrates and reproduces my problem

In Example.java

public class Example {
    private Example(){}
}

And in ExampleTest.java

import java.lang.reflect.Constructor;    

public class ExampleTest{
    Constructor<Example> constructor = Example.class.getDeclaredConstructor();
 }

This gives me an "Unhandled exception: java.lang.NoSuchMethodException" error. As the parameterless constructor exists, I would expect this to work based on the documentation.





Aucun commentaire:

Enregistrer un commentaire