The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
August 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Re: Is It Possible to implement Class references in Java

Posted by Gavin Kingsley on August 17, 2000 at 5:03 PM

Hello Kishori,

Thanks for your reply.

> You have a method and which takes a parameter of a class, say Shape.
> void aMethod ( Shape obj ) ; now since Circle is also a shape then can you pass an object of Circle class to aMethod.
> If this is your quesry then please rean on else never mind.

No, that is not my query. I want to write a method that takes a reference to a Class definition rather than an Object instance. The object has not yet been instantiated. So my method signature might look like:

void aMethod(ClassOfShape classReference) {
Shape myShape = new classReference(int someParameter);
// etc
}

Note that the instance of Shape does not exist before I call aMethod(). I have solved this problem by having an abstract factory:

void aMethodTwo(ShapeFactory factory) {
Shape myShape = factory.giveMeAShape;
// etc
}

But that seems a bit clumsy compared to the Delphi way I am used to. I was just wondering if there is a Java equivalent to the Delphi approach.


Best Regards,
Gavin Kingsley.




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us