Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between loading a class and instantiating it

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.

like image 586
Aniket Thakur Avatar asked Jul 17 '13 07:07

Aniket Thakur


3 Answers

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).

like image 199
JB Nizet Avatar answered Nov 16 '22 16:11

JB Nizet


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.

like image 38
Suresh Atta Avatar answered Nov 16 '22 15:11

Suresh Atta


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)
}
like image 3
Richard Tingle Avatar answered Nov 16 '22 17:11

Richard Tingle