Reputation:
since static variable got memory at the time of class loading,here I am assigning it by creating new A(),which will assign at run time after loading the class.How jvm assign value of new A() at the time of class loading?
public class A{
static A objA =new A();
public static void main(String x[]){}
}
Upvotes: 0
Views: 85
Reputation: 533442
When a class is loaded, the JVM calls a method in the class called <clinit>()V
This method sets all initial values and in your case, it sets the static field.
It can create instances while the method is being called, but it does mean you can't assume in a constructor that all the static fields have been set if you do this.
Upvotes: 2
Reputation: 73528
There's nothing strange here. The class is loaded, then new A()
is created and assigned to objA
. How it actually happens internally is not really relevant, and may vary between JVMs.
Upvotes: 2