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

Posted on 2005-04-13
Last Modified: 2010-03-31
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();



Question by:hank1
    LVL 92

    Expert Comment

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

    Author Comment

    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?
    LVL 92

    Expert Comment

    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.
    LVL 30

    Accepted Solution

    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.
    LVL 30

    Expert Comment

    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).

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now