• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class.forName() method and the new operator

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

What's the difference between the Class.forName() method and the new operator?

Regards,
Shrawan
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shrawan Bhageria:
Hi All,

What's the difference between the Class.forName() method and the new operator?

Regards,
Shrawan



Using Class.forName() you can create objects at runtime.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both can be used for creating objects at runtime. The difference is that Class.forName has the class name as a string parameter, which can be set at runtime. That means the class to be created does not have to be known at compile time (and thus does not even need to be present in the classpath).

Strictly speaking, Class.forName doesn't create an object - it only loads the class, causing static initializers to run. Class.forName("...").newInstance() cause an actaul object to be created.
 
reply
    Bookmark Topic Watch Topic
  • New Topic