Difference between loading a class and instantiating it

后端 未结 5 1431
抹茶落季
抹茶落季 2021-02-08 11:56

Could someone explain what is the difference between Class loading and instantiating a Class. When we load a class with Static variable does it also get instantiated the same ti

相关标签:
5条回答
  • 2021-02-08 12:38

    Class loader actually loads byte code into JVM , runs static initializers.When you want to use static fields within class without creating instance of it ,class must be loaded by class loader first.Default classloader in java is java.lang.ClassLoader.This class loading is done only once.
    While class instantiation is creating instance of class into memory.We can instantiate class using new.Class instantiation can be done as many times. eg: Animal a=new Animal();

    More on class loading

    0 讨论(0)
  • 2021-02-08 12:54

    Class loading
    Whenever the JVM determines it needs a class (to use its static variables, to create a new object, to use its static methods etc) it will load the class and static initialisation blocks will run, static variables are initialised etc. This is (at least under normal circumstances) done only once

    SomeClass.someStaticMethod(); //SomeClass is loaded (if its not already)
    SomeClass.someStaticVariable; //SomeClass is loaded (if its not already)
    SomeClass var=new SomeClass(); //SomeClass is BOTH loaded (if its not already) AND instantiated
    

    As a result the following runs (as an example):

    static Vector3d var=new Vector3d(); //static variables are initialised
    
    static{
        //static initialisation block are run
    }
    

    Instantiating a class
    On the other hand you instantiate a class when you create an instance of the class with the new keyword; instantiating a class is creating an object of the class.

    SomeClass var=new SomeClass(); //SomeClass is instantiating (and loaded if its not already)
    

    As a result the constructor runs:

    public SomeClass(){
    }
    

    and

    {
        //initialisation block(s) also run (but these are very uncommonly used)
    }
    
    0 讨论(0)
  • 2021-02-08 12:55
    Integer.toString(123);
    

    For the above static method call to work, the Integer class must be loaded.

    Integer i = new Integer(123);
    

    And in the above code, I've created an instance (object) of the Integer class (which must also be loaded for this to work, obviously).

    Some classes are not meant to be instantiated (like the Math class, for example, which only has static methods).

    0 讨论(0)
  • 2021-02-08 12:56

    Here is some nice explanation(with an example and observation)

    When a class is loaded and initialized in JVM - Java

    When Class is loaded in Java

    Class loading is done by ClassLoaders in Java which can be implemented to eagerly load a class as soon as another class references it or lazy load the class until a need of class initialization occurs. If Class is loaded before its actually being used it can sit inside before being initialized. I believe this may vary from JVM to JVM. While its guaranteed by JLS that a class will be loaded when there is a need of static initialization.

    When a Class is initialized in Java

    When a Class is initialized in Java After class loading, initialization of class takes place which means initializing all static members of class. A Class is initialized in Java when :

    1) an Instance of class is created using either new() keyword or using reflection using class.forName(), which may throw ClassNotFoundException in Java.

    2) an static method of Class is invoked.

    3) an static field of Class is assigned.

    4) an static field of class is used which is not a constant variable.

    5) if Class is a top level class and an assert statement lexically nested within class is executed.

    Hope that helps.

    0 讨论(0)
  • 2021-02-08 13:02

    A class is loaded when it is referenced (e.g. by Class.forName()).

    You instanciate an object by creating an instance, e.g.

    Object o = new Object();
    

    You can also instanciate an object by using reflection.

    static members of a class are instanciated when the class is loaded, e.g.

    public class Sample {
        private static int variable = 10;
    }
    

    When I now load the class (e.g. by Class.forName("Sample");) Then the variable variable is initialized with the value 10.

    If you are creating a new instance of a class and it is not loaded before the class will be loade before (atomatically). So the construct Class.forName() is only needed under special circumstances (if the class is not known by compile time e.g.).

    0 讨论(0)
提交回复
热议问题