Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.forName() method and the new operator

 
Shrawan Bhageria
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

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

Regards,
Shrawan
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic