Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Importing an object at runtime

Reply
Thread Tools

Importing an object at runtime

 
 
Crouchez
Guest
Posts: n/a
 
      08-30-2007
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()


 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      08-30-2007
Crouchez wrote:
> Class theClass = Class.forName("Object1");
> Object1 o = theClass.newInstance();
> ((Object1)o).go();

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

--
Thomas
 
Reply With Quote
 
 
 
 
Manish Pandit
Guest
Posts: n/a
 
      08-30-2007
On Aug 30, 11:07 am, "Crouchez"
<b...@bllllllahblllbllahblahblahhh.com> wrote:
> 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

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      08-30-2007
Manish Pandit wrote:
> On Aug 30, 11:07 am, "Crouchez"
> <b...@bllllllahblllbllahblahblahhh.com> wrote:
>> 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.


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));

--
Lew
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-01-2007
On Thu, 30 Aug 2007 18:07:51 GMT, "Crouchez"
<> wrote, quoted or indirectly
quoted someone who said :

>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
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-01-2007
On Thu, 30 Aug 2007 18:20:14 GMT, Thomas Fritsch
<> wrote, quoted or indirectly quoted
someone who said :

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

>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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Crouchez
Guest
Posts: n/a
 
      09-02-2007

"Roedy Green" <> wrote in message
news:...
> On Thu, 30 Aug 2007 18:07:51 GMT, "Crouchez"
> <> wrote, quoted or indirectly
> quoted someone who said :
>
>>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


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



 
Reply With Quote
 
Manish Pandit
Guest
Posts: n/a
 
      09-02-2007
On Sep 1, 10:07 pm, "Crouchez" <b...@bllllllahblllbllahblahblahhh.com>
wrote:

> 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

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-02-2007
"Crouchez" wrote, quoted or indirectly quoted someone who said:
>>> 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?


"Roedy Green" wrote:
>> 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


Crouchez wrote:
> I can't the interface's method is protected.


Manish Pandit wrote:
> 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.

<http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.1.5>
> All interface members are implicitly public.


<http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.4>
> 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?

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing an object at runtime Crouchez Java 0 09-03-2007 05:39 AM
Importing at runtime David Poundall Python 6 10-25-2005 05:36 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
How to keep a module with the same name as a module it is importing from importing itself? plb Python 2 02-08-2005 03:14 PM
Error: VBScript runtime (0x800A01A8) Object required: '[object]' Carolyn Speakman XML 1 07-30-2004 07:40 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57