Java – the difference between instantiating and loading a class in Java

javajvmobject-oriented

In Java world, there are some scenarios where I see developers used to load the class rather than instantiation. What is the difference between instantiating and loading a class?

Best Answer

In order for a class to be instantiated it has to be loaded by the classloader...in many cases this is the first time the class has been just-in-time compiled. If you access a static variable on a class, it has to be loaded. If you want to reflect on a class it has to be loaded. There are many circumstances where you might just load a class rather than instantiate.

Also the static initializer(s) for a class fire the first time the class is loaded (and before anything else). If you have an explicit static intializer you can use it to perform a global initialization if need be.