Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Eclipse Class.forname() function(How to use class.forname() for a class having parameterized constructor)
Eclipse Class.forname() function [message #1281968] Tue, 01 April 2014 17:11 Go to next message
SIDDHARTH BHARADWAJ is currently offline SIDDHARTH BHARADWAJFriend
Messages: 1
Registered: April 2014
Junior Member
I have used class.forname() function for creating objects dynamically in eclipse. But for paramaterized constructor i had to class.getconstructor function() for which i need to know the datatypes of constructors.
So, i want to know the way of getting datatypes directly in constructor and use them in class.forname() and class.getconstructor().
This is code which i hv used:
public class ClassOne {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

int param1 = 20;
String className = "ClassTwo";
Class cl = Class.forName(className);
Constructor con = cl.getConstructor(int.class);

Object xyz = con.newInstance(param1);
System.out.println(((ClassBase)xyz).getA());
}
}
Re: Eclipse Class.forname() function [message #1282142 is a reply to message #1281968] Tue, 01 April 2014 23:12 Go to previous message
Russell Bateman is currently offline Russell BatemanFriend
Messages: 3798
Registered: July 2009
Location: Provo, Utah, USA
Senior Member

On 04/01/2014 11:33 AM, SIDDHARTH BHARADWAJ wrote:
> I have used class.forname() function for creating objects dynamically in
> eclipse. But for paramaterized constructor i had to class.getconstructor
> function() for which i need to know the datatypes of constructors. So, i
> want to know the way of getting datatypes directly in constructor and
> use them in class.forname() and class.getconstructor().
> This is code which i hv used:
> public class ClassOne {
> public static void main(String[] args) throws ClassNotFoundException,
> NoSuchMethodException, InstantiationException, IllegalAccessException,
> IllegalArgumentException, InvocationTargetException {
> int param1 = 20;
> String className = "ClassTwo";
> Class cl = Class.forName(className);
> Constructor con = cl.getConstructor(int.class);
> Object xyz = con.newInstance(param1);
> System.out.println(((ClassBase)xyz).getA());
> }
> }

Siddharth

This is pretty just a Java reflection question, isn't it? It's got
nothing to do with Eclipse. You might try javaranch.com, jguru.com or
stackoverflow.com. This forum isn't for what you do with Eclipse, but
how you use Eclipse and trouble using it.

Cheers.
Previous Topic:Apache ODE in eclipse throws sql exception after start
Next Topic:Re: HelloWorld/src
Goto Forum:
  


Current Time: Thu Apr 25 03:33:23 GMT 2024

Powered by FUDForum. Page generated in 0.08717 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top