wood burning stoves 2.0*
The moose likes JDBC 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
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
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
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.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class.forName() method and the new operator
 
Similar Threads
new operator
Class.forName
loading JDBC driver
How many ways can we create an object
using Class.forName() to create object