Class.forName {creating objects on the fly}

Discussion in 'Java' started by gaurav.v.bagga@gmail.com, Dec 8, 2006.

  1. Guest

    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
    , Dec 8, 2006
    #1
    1. Advertising

  2. Hi,

    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
    Ingo R. Homann, Dec 8, 2006
    #2
    1. Advertising

  3. Guest

    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
    , Dec 8, 2006
    #3
  4. Lew Guest

    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
    Lew, Dec 8, 2006
    #4
  5. Daniel Pitts Guest

    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.
    Daniel Pitts, Dec 8, 2006
    #5
  6. Guest

    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
    , Dec 11, 2006
    #6
  7. Hi,

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


    LOL, this brings the factory pattern ad-absurdum!

    Ciao,
    Ingo
    Ingo R. Homann, Dec 11, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Um9tYW4gUGF3bGln?=

    Creating Objects ( response for example ) On The Fly

    =?Utf-8?B?Um9tYW4gUGF3bGln?=, Dec 18, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    352
    Karl Seguin
    Dec 18, 2004
  2. H.MuthuKumaraRajan

    .class and Class.forName

    H.MuthuKumaraRajan, Nov 6, 2003, in forum: Java
    Replies:
    11
    Views:
    1,712
    H.MuthuKumaraRajan
    Nov 7, 2003
  3. cyril
    Replies:
    2
    Views:
    3,834
    cyril
    Aug 25, 2004
  4. Madni
    Replies:
    1
    Views:
    5,870
  5. Brian

    Fly outmenu on the fly

    Brian, Apr 8, 2005, in forum: Javascript
    Replies:
    0
    Views:
    95
    Brian
    Apr 8, 2005
Loading...

Share This Page