Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 39541
    
  27
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 - updated with new look and Ping home screen widget
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class.forName() method and the new operator
 
Similar Threads
loading JDBC driver
Class.forName
new operator
How many ways can we create an object
using Class.forName() to create object