• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Reflection - calling Constructor with Arguments

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I call a constructor with arguments? I don't find any overload for newInstance which takes constructor arguments. I was trying to create an instance using the int argument constructor in the code below.

[This message has been edited by Sri Bala (edited April 07, 2001).]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is why a Java Bean has to have a default constructor and get and set methods for each variable needing to be set.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I don't know about making a bean out of this class, but for what Sri asks, the necessary methods are all there in the java.lang and java.lang.reflect packages:
<code><pre>
import java.lang.reflect.*;
public class Reflect {

private int num;

Reflect() {
num = 1;
}

Reflect(int aNum) {
num = aNum;
}

public void print() {
System.out.println("Number: " + num);
}

public static void main(String args[]) {
Class curr = null;
Object reflect = null;
try {
myClass = Class.forName("Reflect");
Class[] argTypes = {int.class};
Constructor constructor = curr.getDeclaredConstructor(argTypes);
Object[] arguments = {new Integer(5)};
Object instance = constructor.newInstance(arguments);
Method printMethod = curr.getMethod("print", null);
printMethod.invoke(instance, null);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
</pre></code>
Note that to specify a constructor that takes an int, you use the class literal "int.class" rather than "Integer.class". However when you actually invoke the Constructor you need to pass an Integer rather than an int, because the arguments array can only accept Objects, not primitives. Kind of strange, but it works.

[This message has been edited by Jim Yingst (edited April 07, 2001).]
 
Sri Bala
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim . I was looking only at "Class" class. Good to know that Constructor.newInstance(arg) solves the problem. Thomas, I think the JavaBeans requiring default constructor should be a standard as invoking a non default constructor using reflection is not very straight forward.
 
reply
    Bookmark Topic Watch Topic
  • New Topic