Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 253
jayunit100 Avatar asked Nov 23 '11 21:11

jayunit100


1 Answers

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.

like image 109
Bhesh Gurung Avatar answered Sep 20 '22 15:09

Bhesh Gurung