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.

iuser
  • 209
  • 1
  • 6
  • 18
  • 3
    You would need to call a defined constructor. – Oliver Charlesworth Jan 28 '13 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 – Pradeep Simha Jan 28 '13 at 16:04

2 Answers2

1

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

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
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

lucian.pantelimon
  • 3,673
  • 4
  • 29
  • 46