What is the difference between getDeclaredConstructors and getConstructors in the Class API?

JavaReflectionConstructor

Java Problem Overview


I notice that in the Java Reflection API there are two different methods for invoking constructors: the getDeclaredConstructors/getConstructors method. Although the Java docs are slightly different (getDeclaredConstructors seems to imply that it returns ALL constructors, rather than public ones), its not clear why the API explicitly supports these two different methods.

More importantly, I'm wondering : when would one method be preferable to another if we are invoking classes dynamically ? For example, what is the purpose of accessing a private constructor?

Java Solutions


Solution 1 - Java

getDeclaredConstructors (when you want all the constructors) >Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object. These are public, protected, default (package) access, and private constructors.

getConstructors (when you want only public constructors) >Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.

So, looking at the docs for both of them, I think a difference is that getConstructors returns only public constructors while getDeclaredConstructors returns all the constructors (public, protected, default (package) access, and private)

So, it's easy if you need only the public constructors then use getConstructors. Otherwise, if you need all the constructors (disregarding the access-modifier of the constructor) then use getDeclaredConstructors.

Solution 2 - Java

The getDeclaredXX() methods exist for the manipulation of classes in ways that weren't necessarily intended by the maker of those classes. Note that there is a getDeclaredMethod() method that allows you to invoke private methods, and getDeclaredField() method that allows you to get/set private fields.

I'm not completely sure about "legitimate" use cases, but these are obviously useful for doing certain things. Also, this family of methods only returns things specifically declared in the class, not things that exist in the class because of the superclass.

Accessing a private constructor could be useful for the same reasons, I suppose.

Solution 3 - Java

The method 'getDeclaredConstructors' returns only the Constructors that are declared inside the class. The method 'getConstructors' also returns constructors that are not declared inside the class but are inherited from super classes.

So it depends what you're going to do. There is no right approach, it really depends if you also need super constructors.

Solution 4 - Java

I use getDeclaredConstructors to create a factory of classes whose constructor is private to avoid that the "users" of my system can create these classes directly without using the factory. I find this method quite useful.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjayunit100View Question on Stackoverflow
Solution 1 - JavaBhesh GurungView Answer on Stackoverflow
Solution 2 - JavajliView Answer on Stackoverflow
Solution 3 - JavaRoflcoptrExceptionView Answer on Stackoverflow
Solution 4 - JavaRafaView Answer on Stackoverflow