This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
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 Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating objects using Reflection" Watch "Creating objects using Reflection" New topic

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?


Senthil Kumar Sekar
Paul Clapham

Joined: Oct 14, 2005
Posts: 19738

No XML relevance. Moving.
John Jai

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

Joined: Oct 27, 2005
Posts: 20128

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.

How To Ask Questions How To Answer Questions
John Jai

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:
subject: Creating objects using Reflection
jQuery in Action, 3rd edition