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

  • Home
  • SEARCH
  • 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 6129917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:47:53+00:00 2026-05-23T16:47:53+00:00

Possible Duplicate: Difference betweeen Loading a class using ClassLoader and Class.forName AFAIK, two ways

  • 0

Possible Duplicate:
Difference betweeen Loading a class using ClassLoader and Class.forName

AFAIK, two ways are provided in java to init a class from its name.

  • Class

  • public static Class forName(String
    className) throws
    ClassNotFoundException

  • public static Class forName(String
    name, boolean initialize, ClassLoader
    loader) throws ClassNotFoundException

  • ClassLoader:

    public Class loadClass(String
    name) throws ClassNotFoundException {
    return loadClass(name, false);
    }

The known thing is in forName method, we can specify the flag of initialize to be false ,this will skip some static things to be initialized for this class. But what’s else?
And how should I use them correctly?

It’s better you can show some good examples.

Thanks!

UPDATE:

After raised question,I made some simple classLoader test.

ClassLoader cls = ClassLoader.getSystemClassLoader(); 
        Class someClass = cls.loadClass("Test");         
        Class someClass0= Class.forName("Test");        
        Class someClass1= Class.forName("Test",false,cls);


        URL[] urls = new URL[] {new File("bin/").toURL()}; 
        ClassLoader cls2 = new URLClassLoader(urls, null); 
        Class someClass2 = cls2.loadClass("Test");         

        ClassLoader cls3 = new URLClassLoader(urls, cls); 
        Class someClass3 = cls3.loadClass("Test");         

       System.out.println(someClass.equals(someClass0));       
       System.out.println(someClass.equals(someClass1));
       System.out.println(someClass.equals(someClass2)); 
       System.out.println(someClass.equals(someClass3));

The result is

true,true,false,true

UPDATE

Here is my answer about

Difference between loadClass(String name) and loadClass(String name, boolean resolve)

  • 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-23T16:47:54+00:00Added an answer on May 23, 2026 at 4:47 pm

    Consider this code

    class X
    {
        static{   System.out.println("init class X..."); }
    
        int foo(){ return 1; }
    
        Y bar(){ return new Y(); }
    }
    

    The most basic API is ClassLoader.loadClass(String name, boolean resolve)

        Class classX = classLoader.loadClass("X", resolve);
    

    If resolve is true, it will also try to load all classes referenced by X. In this case, Y will also be loaded. If resolve is false, Y will not be loaded at this point.

    There doesn’t seems to be any good reason for resolve=true. If nobody calls X.bar(), Y will never be needed, why should we load it at this point? And if Y is missing or corrupt, we’ll get an error trying to load X, which is totally unnecessary.

    Interestingly, this method is protected, so it’s not easy to invoke it.

    Another method loadClass(name) simply calls loadClass(name,false). It’s public, and it takes the sensible choice of resolve=false. So it is exactly what’s needed by developers.

    ClassLoader only loads classes, it does not initialize classes. We can inspect the class metadata, e.g. its super class, its annotations, its methods and fields, etc. without triggering the static initialization execution. This fact is very important for frameworks.

    Now, Class.forName

    Basically, Class.forName(String name, boolean initialize, ClassLoader loader) calls loader.loadClass(name). And if initialize=true, the class is initialized – in the X example, we’ll see "init class X..." printed.

    Class.forName(name) is the same as forName(name, true, currentLoader).

    Now, why would anyone want to initialize the class at this point? Wouldn’t it be better if the class is initialized only when necessary? A famous use case is JDBC initializing:

        Class.forName("com.mysql.jdbc.Driver");
    

    The convention is, a JDBC driver class registers itself in its static initializer. The above code will trigger the static initialization, making the driver available for subsequent uses.

    From today’s point of view, that design is really odd. We usually don’t rely on static initializers. So there isn’t much justification for initialize=true, and Class.forName(name) should be avoided.

    A “class literal” will return the class, without initializing it

        Class c = com.mysql.jdbc.Driver.class;
        // actually compiled to
        Class c = Class.forName("com.mysql.jdbc.Driver", false, currentLoader);
    

    Now, what the heck is the “currentLoader”? It is the class loader of the current class

    class A
    {
        void foo()
        {
            currenLoader == THIS_A_CLASS.getClassLoader()
        }
    }
    

    When X.bar() is invoked for the first time, a “Y” class is needed. what’s happening is roughly

    class X
        bar()
            // new Y();
    
            Class classY = currentLoader.loadClass("Y");
            Constructor cst = classY.getConstructor();
    
            // next line will initialize Y (if not yet)
            // creating an instance of a class requires it be initialized
            Object y = cst.newInstance();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Calculating the Difference Between Two Java Date Instances hi, I have two
Possible Duplicate: Calculating the Difference Between Two Java Date Instances time1: 17:05 time2: 17:08
Possible Duplicate: What is the difference between these two ways of allocating memory in
Possible Duplicate: Difference between static class and singleton pattern? Which is better in Java,
Possible Duplicate: Calculating the Difference Between Two Java Date Instances In Java, I want
Possible Duplicate: Ruby: difference between || and ‘or’ Using Ruby || and or are
Possible Duplicate: What is the difference between "typename" and "class" template parameters? When defining
Possible Duplicate: Difference between declaring variables before or in loop? Consider the two codes
Possible Duplicate: Difference between using var and not using var in JavaScript For the
Possible Duplicate: Difference between void main and int main? Alright, so I'm using bloodshed

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.