How can i call parameterized constructor if am instantiating a object using "Class.forName("Abc").newInstance()"?

Questions by sachinTest

Showing Answers 1 - 7 of 7 Answers

We can do by the reflection mechanism. For example here is the code.

TestConstructor testConstructor; // This is my class which i want to call.

For arguments of construtor.

Class[] args = new Class[] {String.class} // specify the type of arugument and with no of aruguments.

Declare the value for argument ie. String a1 = "abc";

Object[] argObj = new Object[] {a1}

Get the Construtor object of TestConstructor object i.e

Constructor defaultConstruct = Class.forName("packagename.TestConstructor").getConstructor(args);

TestConstructor test = (TestConstructor) defaultConstruct.newInstance(argObj).

This gives you the class object so u can call repective methods available with the class like test.display();

Hope this answer your question.

Thanks

Naveen.

  Was this answer useful?  Yes

try{

System.out.println("class name is "+class_name);

Class cls=Class.forName(class_name);

Constructor[] ctorlist=cls.getConstructors();

/*

*temporary remove the sysout statement

*

*

*/

/*

* to go to the list box

*

*/

for(int i=0;i<ctorlist.length;i++)

System.out.println(ctorlist[i]+"i="+i);

partypes= ctorlist[inx].getParameterTypes();

Constructor ctor= cls.getConstructor(partypes);

argcnt=partypes.length;

System.out.println("argcnt="+argcnt);

arglist= new Object[argcnt];

/*

*call to verify the argument

*/

boolean temp=comparevalidate();

if(objectlist.size()>0)

System.out.println("object list is ::" + objectlist.toString());

for(int i=0;i<arglist.length;i++)

System.out.println("arglist"+arglist[i].toString());

if(temp==true){

System.out.println("created new object");

Object retobj = ctor.newInstance(arglist);

objectlist.add(retobj);

if(objectlist.size()>0)

System.out.println("object list is new::" + objectlist.toString());

/*else

if((argcnt==0) && (paramValues.size()==0))

{

Object retobjnew = cls.newInstance();

objectlist.add(retobjnew);

System.out.println("inside default constructor");

}*/

}

}catch(Exception ex){ex.printStackTrace();}

  Was this answer useful?  Yes

venkat soma

  • Jan 5th, 2007
 

We cant call parameterized constructors by using newInstance() method. Bcoz, newInstance() method will use default constructor to create an object of a class.

Ex:

public class Sample

{

}

if we write

     (Sample)Class.forName("Sample").newInstance();  -- it will return object of type Sample.

bcoz, we didnt provide any constructor. So compiler will add one default constructor. newInstance() method will make use of that default constructor to create object.

suppose if the Sample class is like this --

class Sample

{

     sample(int a){}

}

(Sample)Class.forName("Sample").newInstance() will rise an error. Bcoz, there is no default constructor.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions