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 time the Class get loaded? After all static code is part of the class rather than it's individual instances. It would be helpful if someone provided an example to help me understand this better.
-
May be an alternative StackExchange site has [this answer](http://programmers.stackexchange.com/questions/53181/what-is-the-difference-between-instantiating-and-loading-a-class-in-java). It is too broad. – AllTooSir Jul 17 '13 at 07:39
-
5@TheNewIdiot I don't think that the question is to broad as it has a very specific scope and also a specific answer. – Uwe Plonus Jul 17 '13 at 07:40
-
SO also defines **broad** as *good answers would be too long for this format.* !!! – AllTooSir Jul 17 '13 at 07:41
5 Answers
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.

- 120,458
- 37
- 198
- 307
-
i am a little confused and want to clarify somethings with you. by lazy loading we mean that even when classloader sees a class declaration inside the current class it doesn't load the other class as it is not initialized yet and while eager loading classloader loads every class that it sees no matter it is initialized or not and can also explain how does both lazy and eager classloaders handle recursive calls(load other classes that they encounter). – Varun Jul 31 '17 at 16:54
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).

- 678,734
- 91
- 1,224
- 1,255
-
+1 This is the shortest but best answer , correct usage of example , this is where experience comes into picture ! – AllTooSir Jul 17 '13 at 08:04
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)
}

- 16,906
- 5
- 52
- 77
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();

- 3,068
- 1
- 20
- 26
-
You are saying Class loader runs static initializers and loading is done exactly once then what happens when an instance of the class changes the value of static variable? – Aniket Thakur Jul 17 '13 at 10:25
-
When the class loader encounters class keyword it will check if class is already loaded or not.As static variable gets initialized when class is loaded ,changes made to it by class will get reflected when you create new instance of class. – Rohan Jul 17 '13 at 10:34
-
Maybe i am not clear. I am asking lets say I created an instance(object) of a class. using this instance I change the value of static variable. How does this change get reflected in the loaded class? – Aniket Thakur Jul 17 '13 at 10:41
-
2When jvm encounters static fields in class,it creates single copy of static variable in memory and all objects refer to it.Consider class contains static field `int a=0;`.We create an 1st object it modifies it to 5.So `a` will have value '5`.Now when we create second object it will have field `a` with value 5 and not 0.Static variables value is part of memory management and class loader has nothing to do with it after class loading. – Rohan Jul 17 '13 at 11:03
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.).

- 9,803
- 4
- 41
- 48
-
1@TheNewIdiot Is posting links a bad thing? Certainly an answer should be able to "live without" links, but a reference adds credibility to an answer – Richard Tingle Jul 17 '13 at 10:01
-
@RichardTingle Sorry , but I wanted to say **just links answer** without explanation. – AllTooSir Jul 17 '13 at 10:03