How can you invoke parameterized constructor to create object from class reference?

Showing Answers 1 - 7 of 7 Answers

angryalla

  • Dec 3rd, 2005
 

parameterized constructors can be invoked just by inserting parameteers into the object create statement i.e for example

class Test{

      Test(int a)

{

 system.out.println("the value of a is"+ a);

}

}

this can be called by calling

Test t1=new Test(a);

  Was this answer useful?  Yes

utpal utkalit

  • Nov 15th, 2006
 

i mean,how would you construct object thru virtual constructor...

  Was this answer useful?  Yes

 Here is the snap shot of the working solution to the said question..

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

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