• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok fine

is there any runtime or dynamic entity is related with newInstance().
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
newInstance() is used to instantiate new objects using Java Reflection. Check this tutorial.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling newInstance() also creates a new object, but not directly, so you will not see a "new" bytecode in your code.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic