Link to home
Start Free TrialLog in
Avatar of hank1
hank1

asked on

new Something() verses Class.forName("Something")

I see Class.forName() used for it's "side effect", which
is to load a class.  What's the difference, in that respect,
from just creating, with new, an instance of that class?  

new Something();

verses

Class.forName("Something");


Thanks
Avatar of Mick Barry
Mick Barry
Flag of Australia image

forName() does not create a new instance of the class, it only loads the class and creates the Class instance
Avatar of hank1
hank1

ASKER

Why would you want to? In the book they load the class
to initialize hardware.  Is loading a class like this more
efficient?  What's the difference in loading a class verses
creating an instance.  They seem very much alike to me.

Again, why would you do this.  Why just load a class?
Various reasons, mostly if you don't actually know the type of class you want to create at compile time. For example if you read it from a config file.

> Why just load a class?

Typically you wouldn't be just loading a class, that class would be being used (and instances of it created) later in the code.
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You would still use the interface-reference for invoking methods, like:

dataAccessObj.updateStatus ( value ) ;

- and calls would result on the actual object it is referring to (instance of OracleDao or SQLServerDao, based on whatever you loaded and created an instance of).