Exforsys
+ Reply to Thread
Results 1 to 2 of 2

Defference between new & Class.newInstance()

This is a discussion on Defference between new & Class.newInstance() within the Java forums, part of the Programming Talk category; Hi All, I would like to know about what is the difference between \"new\" keyword and Class.newInstance(). Any pointers will ...

  1. #1
    ajagania is offline Junior Member Array
    Join Date
    Oct 2004
    Answers
    3

    Defference between new & Class.newInstance()

    Hi All,

    I would like to know about what is the difference between \"new\" keyword and Class.newInstance().

    Any pointers will be greatly appreciated.

    Thanks & Regards,

    Anant.

    Low aim is a crime......


  2. #2
    mahens is offline Junior Member Array
    Join Date
    Jun 2005
    Answers
    1
    When u use the new operator to create an instance of a class, u cant change the class which is been instatiated. In other words, the class which is instantiated is determined at compile time.


    If u use class.newInstance(), then u can change the class which is been instatiated at runtime. In other words for example,

    Class c = Class.forName("Some class Name");

    Object o = c.newInstance();

    Here, u can change the String argument in the class.forName() method and change the class which is getting instatiated. Say u got this String from a property file, then u dont even have to compile the class in order to get another different class instatiated.

    whe u use Object o = new SomeObject(), u cant change the class which is getting instatiated without changing and recompiling again

    Hope u get my point.

    Ask if u have any further queries relating to this

    Regards

    Mahen
    BSc Eng (Comp Sci and Eng), SCJP 1.4


Latest Article

Network Security Risk Assessment and Measurement

Read More...