Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Class.forName {creating objects on the fly}

Reply
Thread Tools

Class.forName {creating objects on the fly}

 
 
gaurav.v.bagga@gmail.com
Guest
Posts: n/a
 
      12-08-2006
Hi All,

Just wanted to know creating instance

Class classDefinition = Class.forName("java.awt.Rectangle");
object = classDefinition.newInstance();

Is this a heavy process in terms of resources and its execution time?

regards
gaurav

 
Reply With Quote
 
 
 
 
Ingo R. Homann
Guest
Posts: n/a
 
      12-08-2006
Hi,

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Just wanted to know creating instance
>
> Class classDefinition = Class.forName("java.awt.Rectangle");
> object = classDefinition.newInstance();
>
> Is this a heavy process in terms of resources and its execution time?


Depends on what you call "heavy" and maybe what JVM you use:

Obviously, it takes more time than calling the Constructor directly,
because there is at least some time needed to "parse" the String
"java.awt.Rectangle".

It also depends on how much the constructor does: if it does some
"heavy" initialisation that takes e.g. 4 secs, then the few extra-millis
of reflection-call do not count.

To answer your question: Measure it!

And, IMHO the question is irrelevant: If you need reflection (because
there is no alternative): Use it. If you do not need it (because there
is another solution): avoid it - because the other solution is 'cleaner'!

Ciao,
Ingo

 
Reply With Quote
 
 
 
 
gaurav.v.bagga@gmail.com
Guest
Posts: n/a
 
      12-08-2006

Hi,

I am new to using reflections and someone told me that using above
piece of code
will really slow down the process(method where I'll create objects
dynamically),so got skeptical of its usage.

Other thing is I dont need it specifically for
"java.awt.Rectangle",was just trying to evaluate
if I do like this will it really hamper the code in which I plan to use
it.

As you said measuring it will be good idea in terms of execution
speed.

regards
gaurav

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      12-08-2006
(E-Mail Removed) wrote:
> Hi,
>
> I am new to using reflections and someone told me that using above
> piece of code
> will really slow down the process(method where I'll create objects
> dynamically),so got skeptical of its usage.
>
> Other thing is I dont need it specifically for
> "java.awt.Rectangle",was just trying to evaluate
> if I do like this will it really hamper the code in which I plan to use
> it.
>
> As you said measuring it will be good idea in terms of execution
> speed.


Execution speed is probably the least important cost of reflection.

Much more important are the loss of compile-time type safety and the
horrendous increase in code complexity. The economics of software now are that
bugs and maintenance costs far outweigh execution costs.

Avoid reflection unless you really, really, really need it. When you do use
it, keep to simple uses like the ones you mentioned (Class.forName(),
newInstance()) and avoid more sophisticated involvement. In very nearly all
cases a good object model and polymorphism will do efficiently and
maintainably what you intended. That said, when you do use this very dangerous
tool called "reflection" it's also very powerful.

It is good that you are researching reflection, but focus more on what it
really does and implies rather than how many more milliseconds it takes. That
way when you do use it you do so fully informed. The speed of reflection will
be largely irrelevant, but the consequences of its use enormous.

- Lew
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      12-08-2006

(E-Mail Removed) wrote:
> Hi All,
>
> Just wanted to know creating instance
>
> Class classDefinition = Class.forName("java.awt.Rectangle");
> object = classDefinition.newInstance();
>
> Is this a heavy process in terms of resources and its execution time?
>
> regards
> gaurav


Look into the Factory object pattern.


Using reflection is very limited...:

public Shape getShapeObject(String shapeClassName) throws Exception {
Class<? extends Shape> classDefinition =
(Class<? extends Shape>)Class.forName(shapeClassName);
return classDefinition.newInstance():
}

getShape("java.awt.Rectangle");

Using the factory object pattern:

public Shape getShapeObject(ShapeFactory shape) {
return shapeFactory.createShape();
}

getShape(new ShapeFactory() {
public Shape createShape() {
return new Rectangle();
}
}


public interface ShapeFactory {
Shape createShape();
}

Sure it looks like more code, but two important things to remember.
First, you won't need to worry so much about class names being wrong,
its checked by the compiler for you. Second, you have a much more
flexible way to get Shapes. If you need to pass in more values to the
constructor, you just do so in the factory.

 
Reply With Quote
 
gaurav.v.bagga@gmail.com
Guest
Posts: n/a
 
      12-11-2006

HI all,

Thanks for your replies it helped me a lot in understanding pros and
cons of reflection.I'll evaluate it and proceed further.
At present just going through it as I have never tried anything in it.

regards
gaurav

 
Reply With Quote
 
Ingo R. Homann
Guest
Posts: n/a
 
      12-11-2006
Hi,

> getShape(new ShapeFactory() {
> public Shape createShape() {
> return new Rectangle();
> }
> }


LOL, this brings the factory pattern ad-absurdum!

Ciao,
Ingo

 
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
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
form objects into class objects =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?= ASP .Net 1 07-06-2006 08:33 AM
Unable to serialize the session state. Please note that non-serializable objects or MarshalByRef objects are not permitted when session state mode is 'StateServer' or 'SQLServer'. Mike Larkin ASP .Net 1 05-23-2005 12:33 PM
Inheritance of objects within objects Simon Elliott C++ 2 12-10-2004 10:59 AM
objects of objects, vectors and sessions bigbinc Java 3 11-18-2003 09:26 AM



Advertisments