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
LVL 1
hank1Asked:
Who is Participating?
 
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
A new instance will be created if you call newInstance ().

>> Why would you want to?

To remove hard-coding. Let's say that you have a Base class or an interface:

public interface IDao // for data-access
{
  public void updateStatus ( int value ) ;
  // etc
}

Let's say that you want to provide two different implementations of this interface - one which writes to an Oracle data-base, and one which writes to a SQL-Server data-base. Let's say the classes are:

public class OracleDao implements IDao // for Oracle data-access

public class SQLServerDao implements IDao // for SQL-Server data-access

- which define implementations of those methods in their own native formats. Now, for one installation, if you want to use Oracle data-base and for another one, you want to use SQL Server - how will you do it if you have hard-coded 'new OracleDao ()' or 'new SQLServerDao ()' in the code?

To overcome that, you can use an IDao reference in your code:

IDao dataAccessObj ;

- and instead of doing: dataAccessObj = new OracleDao () ; or new SQLServerDao () ;, you could use:

dataAccessObj = ( IDao ) Class.forName ( theClassToUse ).newInstance () ;

The value of 'theClassToUse' can be obtained from a properties-file or something, hence you can change that for multiple runs of your application without changing the code.
0
 
objectsCommented:
forName() does not create a new instance of the class, it only loads the class and creates the Class instance
0
 
hank1Author Commented:
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?
0
 
objectsCommented:
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.