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 / General / September 2007

Tip: Looking for answers? Try searching our database.

Importing an object at runtime

Thread view: 
Crouchez - 30 Aug 2007 19:07 GMT
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()
Thomas Fritsch - 30 Aug 2007 19:20 GMT
> Class theClass = Class.forName("Object1");
> Object1 o = theClass.newInstance();
> ((Object1)o).go();
Why not simply this way?
 Object1 o = new Object1();
 o.go();

Signature

Thomas

Roedy Green - 01 Sep 2007 13:20 GMT
On Thu, 30 Aug 2007 18:20:14 GMT, Thomas Fritsch
<i.dont.like.spam@invalid.com> 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.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Manish Pandit - 30 Aug 2007 19:42 GMT
On Aug 30, 11:07 am, "Crouchez"
<b...@bllllllahblllbllahblahblahhh.com> wrote:
> Class theClass = Class.forName("Object1");
> Object1 o = theClass.newInstance();
[quoted text clipped - 9 lines]
>   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
Lew - 30 Aug 2007 21:38 GMT
> On Aug 30, 11:07 am, "Crouchez"
> <b...@bllllllahblllbllahblahblahhh.com> wrote:
[quoted text clipped - 17 lines]
> 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));

Signature

Lew

Roedy Green - 01 Sep 2007 13:18 GMT
On Thu, 30 Aug 2007 18:07:51 GMT, "Crouchez"
<blah@bllllllahblllbllahblahblahhh.com> 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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Crouchez - 02 Sep 2007 06:07 GMT
> On Thu, 30 Aug 2007 18:07:51 GMT, "Crouchez"
> <blah@bllllllahblllbllahblahblahhh.com> wrote, quoted or indirectly
[quoted text clipped - 8 lines]
>
> See http://mindprod.com/jgloss/classforname.html

I can't the interface's method is protected.
Manish Pandit - 02 Sep 2007 06:21 GMT
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
Lew - 02 Sep 2007 17:05 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?

"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.

> 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?

Signature

Lew



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



©2009 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.