0

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.

2
  • 3
    You would need to call a defined constructor. Commented Jan 28, 2013 at 16:04
  • If your class already has at-least one custom constructor, then compiler won't create default no-args constructor. Eg: If your class has only one argument constructor, then you cannot create your object any other way other than passing args to your constructor Commented Jan 28, 2013 at 16:04

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.