Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Creating an object  RSS feed

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Zoram Paul
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rahul,

My doubts are quiet cleared now.
Did you mean dynamic creation of objects?.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!