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
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
forName() does not create a new instance of the class, it only loads the class and creates the Class instance
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?
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.
> 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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).
dataAccessObj.updateStatus
- 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).