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:

Is it possible to implement Class references in Java?

Posted by Gavin Kingsley on August 13, 2000 at 6:12 PM

I come from a Delphi/Object Pascal background. In that language it is easy to define a type

TShapeClass = class of TShape;

I can then have some method that accepts these class references:
procedure SomeMethod(NewShape: TShapeClass)
var
Shape: TShape;
begin
Shape := NewShape.create(X, Y);
{etc}

If TBox is a subclass of TShape, I can pass in a reference to TBox into my SomeMethod and have it construct new instances of that class.

I haven't seen how to do this in Java. Any suggestions? My implementation at the moment uses the Abstract Factory design pattern (someMethod takes a ShapeFactory as a parameter, so it can also accept a BoxFactory) but that seems a little like overkill.



Replies:

Sponsored Links



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