Home > Default > Reflection - class.getConstructor question

Reflection - class.getConstructor question

November 30Hits:0
Advertisement
package my.util;
class Test {
String s;
int i;
public Test(String s, int i){}
I want to do the following
Class c = Class.forName("my.util.Test");
Constructor con = c.getConstructor(new Class[] { String.class, int.class});
Test t = con.newInstance(new Object[] {"al",5});
The above obviously won;t compile because I can;t pass primitives to the object array at the time of creating the instance and I cannot do int.class when getting the
constructor. How do I then use reflection to achieve the
above..?

Answers

The above obviously won;t compile because I can;t
pass primitives to the object array at the time of
creating the instance and I cannot do int.class when
getting the constructor.you can use int.class. this allows you to distinguish between constructors which accept primitive int versus those which might accept the java.lang.Integer type. but to populate the object array of actual arguments to the reflective invocation, you should wrap primitives in their corresponding wrapper type, e.g. int --> java.lang.Integer, char --> java.lang.Character, and so forth.
good luck,
p

Read other 3 answers

Tags:

Related Articles

  • Reflection - class.getConstructor questionNovember 30

    package my.util; class Test { String s; int i; public Test(String s, int i){} I want to do the following Class c = Class.forName("my.util.Test"); Constructor con = c.getConstructor(new Class[] { String.class, int.class}); Test t = con.newInstanc

  • Java reflection (interesting question)November 30

    hi folks, class A { void foo() { Class B overrides method foo() in A class B extends A { void foo() { Now i create a object of class B and assign to A A aref = new B(); aref.foo() will call method foo()of class B. //polymorphism Using reflection, is

  • Flash question...not sure which section to post in...please help the n00b...October 11

    Background: I am a designer (Illustrator/Photoshop) being thrust into the position of creating e-learning materials. I have managed to teach myself the infinitely irritating P.O.S. (piece of software) called Captivate, but am still pretty much useles

  • 1-to-many and collections questionNovember 30

    I'm modeling a standard container and container elements relationship using Kodo. I'm also trying to be fairly RDBMS efficient, so I'm using the kodo inverse properties avoid the extra 'mapping' table. Here is a simplification: class Container privat

  • WSA / CWS Licensing count question.November 30

    Hello, I have a question how WSA and CWS are licensed based on the number of users. Is it based on concurrent or named users, active sessions based on source IP address or another way? This old post mentions concurrent users but based on a legacy iro

  • Questions on Reflexive Access ListsNovember 30

    Hi Sir, I'm trying to protect a server farm using reflexive access lists. I also would like any hosts to originate connections to the servers on TCP ports 23 (telnet) and 25 (smtp). The config on the core router is as follows: int Vlan10 description

  • How to get data into the mySQL database?October 11

    First some background. I have a website that has outgrown its designed dimensions and is a huge burden to maintain. See PPBM5 Benchmark There is a lot of maintenance work involved, so I'm investigating a PHP/MySQL approach to easen the burden and to

  • Coldfusion session variables being lostOctober 11

    ok so when my users login I establish session variables. one of these is employee id. in my Application.cfc I have this at the top: <cfscript> this.name = "My App"; this.sessiontimeout=CreateTimeSpan("0", "0", "45&

  • Facebook not loading properlyOctober 11

    I don't know if this is an issue with Facebook, or an issue with Firefox (maybe someone more "in the know" could tell me), but regardless... I have had issues TWICE THIS WEEK with Firefox not loading Facebook. While it is easily solvable by rese

  • How to prevent IE Caching ???November 30

    Hi AIl, I am using dhtmlX tree , to display tree in first jsp and loading the tree using xml file, but when i go to second jsp and come back to first one, i have some changes in the xml file and thus creating new xml file and string on disk to load t

Copyright (C) 2019 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 14 q. 0.721 s.