What is the difference between getDeclaredConstructors and getConstructors in the Class API?
Solution 1
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
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.
jayunit100
Current: Red Hat BigData, Apache BigTop commiter. Past: Phd in scalable, data driven bioinformatics analytics tools on the JVM, which led me into the world of big data as the genomic data space started to explode. After that, I was with peerindex as a hadoop mapreduce dev, and now I'm a big data engineer at redhat. We're making red hat storage awesome(r). blog: http://jayunit100.blogspot.com. github: http://github.com/jayunit100 pubs : https://www.researchgate.net/profile/Jay_Vyas/publications/?ev=prf_pubs_p2
Updated on June 06, 2022Comments
-
jayunit100 almost 2 years
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?
-
MeBigFatGuy over 12 yearsand by using the constructor.setAccessible(true), you can reflectively call constructors that are private.
-
Mike Nakis about 10 yearsThis is wrong. The getConstructors() methods do _not_ behave similarly to the getMethods() methods. They both fetch constructors of the given class only, ignoring constructors of superclasses. (Their difference is in the visibility of the constructors they fetch.) That's in accordance to the fact that when instantiating a class you must use one of its constructors, you cannot bypass them and directly invoke one of the constructors of its superclass. You can write a couple of test classes to test this. (I did.) Please delete this misleading answer.
-
Paŭlo Ebermann about 9 yearsConstructors are not inherited in Java.
-
neXus over 6 yearsWhile this answer is correct, there is a little caveat. The compiler automatically provides a default (no-argument) public constructor for any class that does not explicitly define any constructors.
getConstructors
will not return this default constructor butgetDeclaredConstructors
does. -
Marco Servetto over 4 yearsHi neXus, are you sure? it looks very creazy that getDeclaredConstructor returns a constructor that was NOT DECLARED. Is it possible that is just an issue with the visibility of the "autodeclared" constructor?
-
Per Lundberg over 2 yearsMy testing right now:
getDeclaredConstructor()
andgetConstructor()
both returns compiler-created (default) constructors. So I think your guess might be correct @MarcoServetto.