GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Tech FAQs  >  Programming  >  Java
Go To First  |  Previous Question  |  Next Question 
 Java  |  Question 334 of 928    Print  
how can you invoke parameterized constructor to create object from class reference?

  
Total Answers and Comments: 3 Last Update: November 16, 2006     Asked by: utpal utkalit 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
December 03, 2005 11:05:57   #1  
angryalla Member Since: November 2005   Contribution: 1    

RE: how can you invoke parameterized constructor to cr...

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);


 
Is this answer useful? Yes | No
November 15, 2006 00:46:56   #2  
utpal utkalit        

RE: how can you invoke parameterized constructor to cr...
i mean how would you construct object thru virtual constructor...
 
Is this answer useful? Yes | No
November 16, 2006 06:17:09   #3  
utpal_utkalit Member Since: November 2005   Contribution: 3    

RE: how can you invoke parameterized constructor to cr...

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();}


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape