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 7008911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:47:45+00:00 2026-05-27T21:47:45+00:00

Does this two Java statement lookup the class in the same way? this.getClass().getClassLoader().loadClass(Foo); and

  • 0

Does this two Java statement lookup the class in the same way?

this.getClass().getClassLoader().loadClass("Foo");

and

Class.forName("Foo");
  • 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-27T21:47:46+00:00Added an answer on May 27, 2026 at 9:47 pm

    They’re different: even though they use the same class loader (see forName() documentation) loadClass() does not run static initializers while forName() does.

    This is easily demonstrated with the following classes:

    public class ClassA {
      static {
        System.out.println("ClassA static initializer");
      }
    
      public ClassA() {}
    }
    

    and

    public class ClassB {
      private ClassB() {}
    
      private void loadStuff(boolean initialize) throws ClassNotFoundException {
        if (initialize) {
          Class.forName("ClassA");
        } else {
          this.getClass().getClassLoader().loadClass("ClassA");
        }
      }
    
      public static void main(String[] args) throws ClassNotFoundException {
        ClassB b = new ClassB();
        b.loadStuff(true); // Try also false
      }
    }
    

    Running ClassB passing true to loadStuff() will display “ClassA static initializer” while running it passing false to loadStuff() doesn’t display anything.

    Of course, if you subsequently instantiate the loaded class its static initializer will be run if it has not yet been executed. In this case the difference between the two methods of loading a class is in the time when static initializers are run.

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

Sidebar

Related Questions

Does your work environment use Harvest SCM? I've used this now at two different
This is actually a two part question. First,does the HttpContext.Current correspond to the current
What does this mean exactly? I'm doing something like this: File.Copy(@\\foo\bar\baz.txt, @c:\test\baz.txt); MSDN doesn't
Why does this equation produce two different values for e in C (32-bit) and
I accidentally wrote a java statement with two semicolons at the end. The java
I've two different classes: Cliente.java public class Cliente { private static String user; private
This is closely related to, but is not the same question as: Java -
Does it impact memory if I have two Java classes that have native calls
I have two Java interfaces and one implementing class. (I have used Eclipse to
Does this pattern: setTimeout(function(){ // do stuff }, 0); Actually return control to the

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.