Skip to main content



      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 13:11 Go to next message
Eclipse UserFriend
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 19:12 Go to previous message
Eclipse UserFriend
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: Sun Jun 15 10:42:24 EDT 2025

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

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

Back to the top