Importing an object at runtime

C

Crouchez

Class theClass = Class.forName("Object1");
Object1 o = theClass.newInstance();
((Object1)o).go();

I need to wrap the object as an Object1 to be able to call go(). If I am
reading the classes from disk and don't hardcode the classes into
Class.forName("????") how do I then wrap the resulting object?

Something like this:

Class theClass = Class.forName(file);
Object o = theClass.newInstance();
((obj.getClass().getName())o).go(); //can't do obj.getClass().getName()
 
T

Thomas Fritsch

Crouchez said:
Class theClass = Class.forName("Object1");
Object1 o = theClass.newInstance();
((Object1)o).go();
Why not simply this way?
Object1 o = new Object1();
o.go();
 
M

Manish Pandit

Class theClass = Class.forName("Object1");
Object1 o = theClass.newInstance();
((Object1)o).go();

I need to wrap the object as an Object1 to be able to call go(). If I am
reading the classes from disk and don't hardcode the classes into
Class.forName("????") how do I then wrap the resulting object?

Something like this:

Class theClass = Class.forName(file);
Object o = theClass.newInstance();
((obj.getClass().getName())o).go(); //can't do obj.getClass().getName()

How are you sure that "go()" is implemenented by the object you're
trying to instantiate? I believe this can be done in a cleaner way,
where you instantiate the object, and use instanceOf to make sure the
instance is implementing the interface which has "go()", cast it as
that interface (which is allowed) and then call the method go() on it.

-cheers,
Manish
 
L

Lew

Manish said:
How are you sure that "go()" is implemenented by the object you're
trying to instantiate? I believe this can be done in a cleaner way,
where you instantiate the object, and use instanceOf to make sure the
instance is implementing the interface which has "go()", cast it as
that interface (which is allowed) and then call the method go() on it.

Example:

public interface Goer
{
public void go();
}

public class DoesGo implements Goer
{
public void go() { ... }
}
....
// inside some other method or class:

Goer goer = new DoesGo();
// can also instantiate with
// Class<? extends Goer>.forName( "DoesGo" ).newInstance));
 
R

Roedy Green

I need to wrap the object as an Object1 to be able to call go(). If I am
reading the classes from disk and don't hardcode the classes into
Class.forName("????") how do I then wrap the resulting object?

Normally your dynamic class will implement an interface. You can then
cast the object to that interface and call its method.

See http://mindprod.com/jgloss/classforname.html

Failing that, you get bogged in the horrors of
introspection/reflection.

see http://mindprod.com/jgloss/reflection.html
 
R

Roedy Green

Why not simply this way?
Object1 o = new Object1();
o.go();

If he knew the name of Object1 at compile time he would not need to
dick around with Class.forName. You only need this whacky stuff when
you don't know the name of the class at compile time.
 
M

Manish Pandit

I can't the interface's method is protected.

Why/How is the interface's method protected?

AFAIK, you cannot even compile an interface with a protected method.
You can have default (none), public or abstract modifiers only.

-cheers,
Manish
 
L

Lew

, quoted or indirectly quoted someone who said:
I can't the interface's method is protected.

Manish said:
AFAIK, you cannot even compile an interface with a protected method.
You can have default (none), public or abstract modifiers only.

All interface methods are public abstract. You may redundantly declare them
so, but you may not contradict these modifiers. They are never package-private.

All interface members are implicitly public.

Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.

Every method declaration in the body of an interface is implicitly public.

So, Crouchez, what is the difficulty?
 

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

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top