Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 565227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:50:59+00:00 2026-05-13T12:50:59+00:00

What is the difference between Class.forName() and Class.forName().newInstance() ? I do not understand the

  • 0

What is the difference between Class.forName() and Class.forName().newInstance()?

I do not understand the significant difference (I have read something about them!). Could you please help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:51:00+00:00Added an answer on May 13, 2026 at 12:51 pm

    Maybe an example demonstrating how both methods are used will help you to understand things better. So, consider the following class:

    package test;
    
    public class Demo {
    
        public Demo() {
            System.out.println("Hi!");
        }
    
        public static void main(String[] args) throws Exception {
            Class clazz = Class.forName("test.Demo");
            Demo demo = (Demo) clazz.newInstance();
        }
    }
    

    As explained in its javadoc, calling Class.forName(String) returns the Class object associated with the class or interface with the given string name i.e. it returns test.Demo.class which is affected to the clazz variable of type Class.

    Then, calling clazz.newInstance() creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. In other words, this is here actually equivalent to a new Demo() and returns a new instance of Demo.

    And running this Demo class thus prints the following output:

    Hi!
    

    The big difference with the traditional new is that newInstance allows to instantiate a class that you don’t know until runtime, making your code more dynamic.

    A typical example is the JDBC API which loads, at runtime, the exact driver required to perform the work. EJBs containers, Servlet containers are other good examples: they use dynamic runtime loading to load and create components they don’t know anything before the runtime.

    Actually, if you want to go further, have a look at Ted Neward paper Understanding Class.forName() that I was paraphrasing in the paragraph just above.

    EDIT (answering a question from the OP posted as comment): The case of JDBC drivers is a bit special. As explained in the DriverManager chapter of Getting Started with the JDBC API:

    (…) A Driver class is loaded, and
    therefore automatically registered
    with the DriverManager, in one of two
    ways:

    1. by calling the method Class.forName. This explicitly loads
      the driver class. Since it does not
      depend on any external setup, this way
      of loading a driver is the recommended
      one for using the DriverManager
      framework. The following code loads
      the class acme.db.Driver:

       Class.forName("acme.db.Driver");
      

    If acme.db.Driver has been written so that loading it causes an
    instance to be created and also calls
    DriverManager.registerDriver with that
    instance as the parameter
    (as it
    should do), then it is in the
    DriverManager‘s list of drivers and
    available for creating a connection.

    1. (…)

    In both of these cases, it is the responsibility of the newly-loaded Driver class to register itself by calling DriverManager.registerDriver. As mentioned, this should be done automatically when the class is loaded.

    To register themselves during initialization, JDBC driver typically use a static initialization block like this:

    package acme.db;
    
    public class Driver {
    
        static {
            java.sql.DriverManager.registerDriver(new Driver());
        }
        
        ...
    }
    

    Calling Class.forName("acme.db.Driver") causes the initialization of the acme.db.Driver class and thus the execution of the static initialization block. And Class.forName("acme.db.Driver") will indeed "create" an instance but this is just a consequence of how (good) JDBC Driver are implemented.

    As a side note, I’d mention that all this is not required anymore with JDBC 4.0(added as a default package since Java 7) and the new auto-loading feature of JDBC 4.0 drivers. See JDBC 4.0 enhancements in Java SE 6.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the difference between new operator and Class.forName(...).newInstance() ? Both of them create
Can someone explain me the difference between Class.forName() and Thread.currentThread().getContextClassLoader().loadClass(). I have next code
Possible Duplicate: What is difference between “Class.forName()” and “Class.forName().newInstance()”? In my android apps to
What is the difference between these two class declarations? I don't understand why @class
Whats the difference between class methods and Instance methods. Why do we need them
Recently I've been thinking about performance difference between class field members and method variables.
I have a basic best-practices question in Objective C. I understand the difference between
Can anyone tell me about the difference between class variables and class instance variables?
I know the difference between class methods and object methods but I am not
Possible Duplicate: Difference between static class and singleton pattern? I'm thinking about the choice

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.