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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:20:27+00:00 2026-05-26T03:20:27+00:00

I cannot easily find it in JLS/JVMSpec, nor in SO. I’m sure it must’ve

  • 0

I cannot easily find it in JLS/JVMSpec, nor in SO. I’m sure it must’ve been asked…

So, what does “new” do actually? Suppose we instantiate a class B in A:

class A {
    // ...
    new B();
    // ...
}

Is this equivalent to

class A {
    // ...
    A.class.getClassLoader().loadClass("B's canonical name").newInstance();
    // ...
}

?

Does it, or does it not work like that in every environment?

I’d be grateful if you can point me to the appropriate chapter in JLS/JVMSpec. Thanks!

Edit: surely we can’t call B.class.getCanonicalName() in loadClass() call, since B’s not loaded yet. JVM has to resolve the name based on the import statement.

  • 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-26T03:20:27+00:00Added an answer on May 26, 2026 at 3:20 am

    Is this equivalent to

    class A {
        // ...
        A.class.getClassLoader().loadClass("B's canonical name").newInstance();
        // ...
    }
    

    ?

    No, not always.

    Class loading is performed only once for a given namespace, unless the Class in question has been previously unloaded. Therefore, the equivalent expression A.class.getClassLoader().loadClass("B's canonical name") will be executed only once in most scenarios. In other words, if you have two expressions – new A(), the loadClass will be performed only once.

    Invocation of a constructor is treated as a method invocation by the JVM, but this requires the cooperation of a Java compiler. The JVM and the compiler have to adhere to section 3.9 of the Java Virtual Machine Specification, which states:

    3.9 Specially Named Initialization Methods

    At the level of the Java virtual machine, every constructor (§2.12)
    appears as an instance initialization method that has the special name
    <init>. This name is supplied by a compiler. Because the name <init>
    is not a valid identifier, it cannot be used directly in a program
    written in the Java programming language. Instance initialization
    methods may be invoked only within the Java virtual machine by the
    invokespecial instruction, and they may be invoked only on
    uninitialized class instances. An instance initialization method takes
    on the access permissions (§2.7.4) of the constructor from which it
    was derived.

    A class or interface has at most one class or interface initialization
    method and is initialized (§2.17.4) by invoking that method. The
    initialization method of a class or interface is static and takes no
    arguments. It has the special name <clinit>. This name is supplied by
    a compiler. Because the name <clinit> is not a valid identifier, it
    cannot be used directly in a program written in the Java programming
    language. Class and interface initialization methods are invoked
    implicitly by the Java virtual machine; they are never invoked
    directly from any Java virtual machine instruction, but are invoked
    only indirectly as part of the class initialization process.

    This section assumes that the Class object pertaining to the class in question is available to the current thread. Once the Class object is available, the method <init> corresponding to the constructor with the right set of arguments, will be invoked.

    The question of which classloader will be used to load the class, if not already loaded, is a bit different, and has nothing to do with the new keyword. It depends on how one class references another i.e. does a symbolic reference need to be resolved in the runtime constant pool? The behavior in this context is defined in Section 5.3 of the Java Virtual Machine Specification:

    5.3 Creation and Loading

    Creation of a class or interface C denoted by the name N consists of
    the construction in the method area of the Java virtual machine
    (§3.5.4) of an implementation-specific internal representation of C.
    Class or interface creation is triggered by another class or interface
    D, which references C through its runtime constant pool.

    …

    The Java virtual machine uses one of three procedures to create class
    or interface C denoted by N:

    • If N denotes a nonarray class or an interface, one of the two following methods is used to load and thereby create C :

      • If D was defined by the bootstrap class loader, then the bootstrap
        class loader initiates loading of C (§5.3.1).

      • If D was defined by a user-defined class loader, then that same
        user-defined class loader initiates loading of C (§5.3.2).

    • Otherwise N denotes an array class. An array class is created directly by the Java virtual machine (§5.3.3), not by a class loader.
      However, the defining class loader of D is used in the process of
      creating array class C.

    Note the sentence – If D was defined by a user-defined class loader, then that same user-defined class loader initiates loading of C in the above quote. In the context of the expression new A(), the classloader that loaded the enclosing class will be responsible for loading A in accordance with the VM Spec; this is of course, assuming that the enclosing class is not loaded by the bootstrap classloader.

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

Sidebar

Related Questions

I cannot find a way that easily lets me create a new file, treat
Put simple, I cannot find a way to easily display furigana in a JLabel.
Cannot find any example on Google.
I cannot find a maven repository for Lucene 4.0. I don't want to use
I am trying to use the NumberPicker control. But I cannot find any clear
So when I create a new Qt project inside Qt Creator I'm only asked
I find myself assigning this to a variable so I can easily use it
I know this question has been asked alot of times, however most of them
Folks, is there any smart way to easily find unused files in entire solution?
I'm trying to do a dead simple thing but cannot find an effective way

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.