iuser
iuser

Reputation: 209

Creating an object without a default constructor

I have a package which has some N number of classes and I'm scanning all the classes and initializing them through a method. All the classes with a default constructor are being initialized but the ones without default(zero argument) constructor throws an exception. Does anyone know how to create an object without default constructor?

P.S. I need a java code.

Upvotes: 0

Views: 3087

Answers (2)

lucian.pantelimon
lucian.pantelimon

Reputation: 3669

You can try looking here. It explains how to create objects using Java reflection.

Or just Google: java constructor reflection. I got this one using the "I'm feeling lucky" feature

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 360066

Use Class#getConstructors() to find a defined constructor, and call that instead.

Upvotes: 1

Related Questions