File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes When to use new and newInstance() and what is the difference bet them. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "When to use new and newInstance() and what is the difference bet them." Watch "When to use new and newInstance() and what is the difference bet them." New topic
Author

When to use new and newInstance() and what is the difference bet them.

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
As both creates new object.

Example:

1) String str = new String();
2) Class clazz = Class.forName("Demo");
Demo demo = (Demo) clazz.newInstance();

So what is the difference between them , how they behave internally.

T. Huy Nguyen
Ranch Hand

Joined: Nov 02, 2010
Posts: 57
An obvious difference is that the class's fully qualified name can be dynamically constructed/read from file/changed when you use "newInstance".

"newInstance" also requires the Java class to have an no-arg constructor, else you will get error at runtime. If you do the same with "new", you will immediately get error at compile time.


My material for SCJP (2008), SCWCD (2010), SCBCD (2010). About me
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
ok fine

is there any runtime or dynamic entity is related with newInstance().
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

newInstance() is used to instantiate new objects using Java Reflection. Check this tutorial.


[My Blog]
All roads lead to JavaRanch
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
I am getting java.lang.NoSuchMethodException: instance1.<init>(instance1):
on the below line of code



while this is working for java.lang.String why ?





is i am doing anything wrong ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Prabhat Ranjan wrote:is i am doing anything wrong ?

No, of course not.

However I suppose it's possible that you want your code to execute without producing that error. If that's the case then obviously you are doing something wrong.

You'll notice that String has a constructor

That's the one you are executing via reflection. Presumably your instance1 class doesn't have a constructor

That's the one you are trying to execute via reflection.

So you have two options. You could create such a constructor, or you could change your code to find a constructor which does exist.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Yes you are very correct.

i declared the parametrized constructor in class then it is working fine.



this code is working fine.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
but if we see the byte code of Class.forName("java.lang.String").newInstance();

it doesn't create new block.

while if we create Simple object with new operator, like String str = new String();
it creates new block.

so what does it mean newInstance() doesn't create new object , please confirm me.

Byte code for the same:



Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

Calling newInstance() also creates a new object, but not directly, so you will not see a "new" bytecode in your code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
good then i understood now..it might be doing internally via reflection at runtime which is not visible in bytecode even.

Thanks

below is even the good example which calls the no arg constructor means its creating new Object.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When to use new and newInstance() and what is the difference bet them.