This is a discussion on Defference between new & Class.newInstance() within the Java Tutorials forums, part of the Articles and Tutorials category; Hi All, I would like to know about what is the difference between \"new\" keyword and Class.newInstance(). ...
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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...... |
|
|||
|
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 |