Class.forName {creating objects on the fly}

G

gaurav.v.bagga

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
 
I

Ingo R. Homann

Hi,

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
 
G

gaurav.v.bagga

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
 
L

Lew

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
 
D

Daniel Pitts

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

gaurav.v.bagga

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
 
I

Ingo R. Homann

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

LOL, this brings the factory pattern ad-absurdum!

Ciao,
Ingo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top