[Logo]
Forums Register Login
Creating an object
Hello folks!

Can anyone tell me the difference between creating objects like

Test7 t1=(Test7)Class.forName("Test7").newInstance();
Test7 t2=(Test7)Class.forName("Test7").newInstance();

and

Test7 t1 = new Test7();
Test7 t2 = new Test7();

When and where shall should i use them?

Thank you.

Hi Zoram,

I do not think this question is anyway related to JSP.Please choose forum properly before posting.

Coming to the question , the first one uses reflection to create objects.

Its useful in certain conditions where at runtime you need to decide which object to create according to some parameters.In that case you can make well use of reflection to create object of desired type.
Thank you Rahul,

My doubts are quiet cleared now.
Did you mean dynamic creation of objects?.
 

Originally posted by Zoram Paul:
Did you mean dynamic creation of objects?.



The parameter to forName can be decided at runtime for creation of different object.
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database


This thread has been viewed 563 times.

All times above are in ranch (not your local) time.
The current ranch time is
Mar 23, 2019 06:19:10.