Dynamic object instantiation without 'new' - PLEASE HELP

G

Gandu

Could some Java Guru please help me? I have the following problem:
Given the name of a class(e.g. MyClass), I can use use the methods of
the Class class as:
Class myClass = Class.forName(MyClass);
Object obj = myClass.newInstance();
The problem here is that the type of obj is Object, NOT MyClass. Of
course, I can cast it as
MyClass myClassNew = (MyClass)obj;
Now, my question is:
How do I do this dynamically? That is, suppose for example I am looking
at all Java classes in a directory. I pick out a Java class(check if the
file name extension is .java) and then I want to create an instance of it.
The class name is obviously stored in some String variable - but how do I
use it to create an instance of the specified class, NOT Object. To be
more specific, suppose I have the method:
public Object getInstance(String className){
try{
Class cls = Class.forName(className);
return cls.newInstance();
}
catch(Exception ex){
//
}
return null;
}

Now how do I re-cast the object returned(of type Object) to its original
type(stored in String className).
Any help would be greatly appreciated.
 
C

Christophe Vanfleteren

Gandu said:
Could some Java Guru please help me? I have the following problem:
Given the name of a class(e.g. MyClass), I can use use the methods of
the Class class as:
Class myClass = Class.forName(MyClass);
Object obj = myClass.newInstance();
The problem here is that the type of obj is Object, NOT MyClass. Of
course, I can cast it as
MyClass myClassNew = (MyClass)obj;
Now, my question is:
How do I do this dynamically? That is, suppose for example I am looking
at all Java classes in a directory. I pick out a Java class(check if the
file name extension is .java) and then I want to create an instance of
it. The class name is obviously stored in some String variable - but how
do I use it to create an instance of the specified class, NOT Object. To
be more specific, suppose I have the method:
public Object getInstance(String className){
try{
Class cls = Class.forName(className);
return cls.newInstance();
}
catch(Exception ex){
//
}
return null;
}

Now how do I re-cast the object returned(of type Object) to its original
type(stored in String className).
Any help would be greatly appreciated.

You can't. But you could call methods on the object if you know they're
there. Just let the classes you instantiate that way implement a common
interface / inherit from the same class, and cast back to that
interface/class.

Or, if even that is not possible, you can use reflection to call methods.

But what exactly are you trying to achieve here?
 
P

paul brown

Hi,

on this theme...

assuming it's unwise to call Class.forName, because this may fail in app
servers/servlet containers/etc, where you might not have the
classpath you'd expect in a stand-alone program...are you supposed to forego
the benefits of dynamic loading?

or do we use serialization/interfaces instead? so, instead of using
Class.forName("someclass"), you would have to be passed a stream from
somewhere else...maybe a socket(?) and then use ObjectInputStream to pull in
an Object, which you'd downcast to specific interfaces
that you assume will be supported.

are there other ways to load (as yet unseen) implementations dynamically
without Class.forName?

Thanks,
Paul
 
J

Jon A. Cruz

Gandu said:
The class name is obviously stored in some String variable - but how do I
use it to create an instance of the specified class, NOT Object. To be
more specific, suppose I have the method:

You HAVE created an instance of that class, not of Object.

Try calling .getClass().getName() on the returned object to see what it is.
 
C

Chris Uppal

paul said:
are there other ways to load (as yet unseen) implementations dynamically
without Class.forName?

There's java.lang.ClassLoader.defineClass().

-- chris
 
L

Lee Fesperman

paul said:
on this theme...

assuming it's unwise to call Class.forName, because this may fail in app
servers/servlet containers/etc, where you might not have the
classpath you'd expect in a stand-alone program...are you supposed to forego
the benefits of dynamic loading?

No, you don't have to forego the benefits of dynamic loading. You just have to ensure
that any classes that you load dynamically are on the classpath for the container
software.
or do we use serialization/interfaces instead? so, instead of using
Class.forName("someclass"), you would have to be passed a stream from
somewhere else...maybe a socket(?) and then use ObjectInputStream to pull in
an Object, which you'd downcast to specific interfaces
that you assume will be supported.

You can't get around it that way. Any classes (and all their super classes and
implemented interfaces and the same for any referenced classes/interfaces) created
through ObjectInputStream must be on the classpath ... or, more accurately, must be
available to the current classloader.
are there other ways to load (as yet unseen) implementations dynamically
without Class.forName?

Yes, but you must still deal with the implications above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top