Difference between loading a class and instantiating it

后端 未结 5 1423
抹茶落季
抹茶落季 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

提交回复
热议问题