File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 41839
    
  63
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