Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

Having a little problem with reflection

Thread view: 
fishfry - 02 Mar 2004 05:41 GMT
       Class c = Class.forName(classname);
       classname c = new classname();

Doesn't seem to compile. My understanding is that the first line creates
c as a class whose name is classname. But I can't seem to use line 2 to
instantiate an object of that class. Do I have to use getConstructor()?
I can't seem to find any specific examples of this in the Sun tutorials,
perhaps someone can straighten me out. Thanks in advance.
Tony Morris - 02 Mar 2004 06:06 GMT
>         Class c = Class.forName(classname);
>         classname c = new classname();
>
> Doesn't seem to compile. My understanding is that the first line creates
> c as a class whose name is classname.
The class loader simply loads the class called classname.
A reference to the class is returned and assigned to 'c'.

> But I can't seem to use line 2 to
> instantiate an object of that class.
Why not ? There is nothing special you are doing (that we can see) that
prevents such a thing.
What is the compile-time error ?

> Do I have to use getConstructor()?
No.

> I can't seem to find any specific examples of this in the Sun tutorials,
> perhaps someone can straighten me out. Thanks in advance.

You are performing a dynamic class load, followed by a static instantiation.
Very odd. Do you mean to instantiate the Class object ?
Use the newInstance() method.
If you haven't got a default constructor (speculating that this is the
source of your compile-time error), then this will cause an exception.
More information required (the compile-time error).

Signature

Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform

fishfry - 02 Mar 2004 08:22 GMT
> >         Class c = Class.forName(classname);
> >         classname c = new classname();
[quoted text clipped - 22 lines]
> source of your compile-time error), then this will cause an exception.
> More information required (the compile-time error).

Ok I have a dynamic instantiation going like so:

   Class c = Class.forName(classname);
   Object o = c.newInstance();  

That is working ok. However now I need to pass an argument to the
constructor:

  Object o = c.newInstance("foo")

The compiler complains "newInstance() in java.lang.Class cannot be
applied to (java.lang.String)"
Collin VanDyck - 02 Mar 2004 16:15 GMT
> Ok I have a dynamic instantiation going like so:
>
[quoted text clipped - 8 lines]
> The compiler complains "newInstance() in java.lang.Class cannot be
> applied to (java.lang.String)"

Try using the Class.getDeclaredConstructor(Class[] parameterTypes) to get
the Constructor.
ex:
Class[] parameterTypes = new Class[1];
parameterTypes[0] = "".getClass();
Constructor constructorWithStringParam =
c.getDeclaredConstructor(parameterTypes);

Once you have the Constructor, use

Constructor.newInstance(Object[] initargs)
ex:
Object[] initargs = new Object[1];
initargs[0] = "first param";
Object o = constructorWithStringParam.newInstance(initargs);

To call the constructor with the String argument.

-CV
Thomas Weidenfeller - 02 Mar 2004 08:20 GMT
>         Class c = Class.forName(classname);
>         classname c = new classname();
>
> Doesn't seem to compile.

Provide REAL code and the REAL error message.

/Thomas


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.