my dog learned polymorphism*
The moose likes Java in General and the fly likes Creating objects using Reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating objects using Reflection" Watch "Creating objects using Reflection" New topic
Author

Creating objects using Reflection

Senthil Kumar Sekar
Ranch Hand

Joined: Aug 08, 2010
Posts: 45

Why is that a constructor is made mandatory for POJO-->For instance hibernate asks the developer to have a constructor in the POJOs.

Can anybody what are the other ways a f/w or any container does to create instances other than below



and my POJO is



In this case i do not hav a default constructor and iam able to create instances as shown above without any issues.

Then why we should have a contructor as a mandatory?

Thanks.

Regards
Senthil Kumar Sekar
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18711
    
    8

No XML relevance. Moving.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
It seems that Hibernate uses Reflection Constructor methodology to create an instance. Try the below code and check for the error with your POJO.

The code fails when the getConstructor() method is called with the Pojo class
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

The reason being that the automatically generated constructor has the same visibility as the class itself, in this case default visibility (no access modifier). Make the class public and that code will work.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Yes Rob. it worked perfect. there should be some other reason for Hibernate forcing to have default constructors in POJO then.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating objects using Reflection