Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Difference Class.getConstructor and Class.getDeclaredConstructor Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Difference Class.getConstructor and Class.getDeclaredConstructor" Watch "Difference Class.getConstructor and Class.getDeclaredConstructor" New topic
Author

Difference Class.getConstructor and Class.getDeclaredConstructor

D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 471
Generally problem is:
I have a class with protected default constructor, like:

I want to be able to instantiate this class, however A.class.getConstructor() throws an exception no such method. Will getDeclaredConstructor help? I notice that ReadObject implementation uses getDeclaredConstructor with following changing accessability.


Get power of your iPod with MediaChest | Minimal J2EE container is here | Light weight full J2EE stack | My blog | Co-author of "Windows programming in Turbo Pascal"
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

getConstructor will return a Constructor reference only for a public constructor. But getDeclaredConstructor should return a Constructor reference regardless of the constructor's access level. However, to actually invoke that constructor (e.g., calling newInstance), you would need the proper access.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 471
Thanks, it was what I'd like to hear.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Difference Class.getConstructor and Class.getDeclaredConstructor
 
Similar Threads
Does java support Protected classes?
what is mean mappedBy
Top level class??
probably statement of the obvious regarding class member access
what us mean mappedBy="item" ?