aspose file tools
The moose likes JDBC and Relational Databases and the fly likes Class.forName() method and the new operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Class.forName() method and the new operator" Watch "Class.forName() method and the new operator" New topic
Author

Class.forName() method and the new operator

Shrawan Bhageria
Ranch Hand

Joined: Jun 20, 2005
Posts: 52
Hi All,

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

Regards,
Shrawan
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42952
    
  73
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class.forName() method and the new operator