Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an object of a class in its own static initializer

As per JLS:

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

T is a class and an instance of T is created.

Also it says,

Initialization of a class consists of executing its static initializers and the initializers for static fields (class variables) declared in the class

I infer two points from this

  • Class initialization consists of executing its static initializers
  • Class initialization happens if a class is instantiated.

Now,

I assume when I create an object of a class Test in its own (Test's own) static initializer it should throw me a stack overflow as it should call itself repeatedly because as per the above two points, instantiation of the class should initialize the class and initialization block has the instantiation of the class. A stack overflow occurs when I instantiate the class in its own constructor or in its own instance initializers.

For example,

public class Test {

    static{
        System.out.println("static test");
        new Test();
    }
    {
       //new Test(); // This will give a stack overflow
        System.out.println("intializer");
    }

    public Test(){
        //new Test(); // This will give a stack overflow
        System.out.println("constructor");
    }
    public static void main(String[] args) {
    }

}

However the result is something different.

Result:

static test intializer constructor

Either I am too confused understanding the initialization of the class or I do apologize if I am missing something very obvious here and appreciate your help.

like image 616
chebus Avatar asked Jul 20 '15 02:07

chebus


People also ask

Can we create object of static method?

Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.

Can we create a object in static block?

The static block is a static initializer (a class initializer). You can use it to initialize a class or to do some logic during class load. If you remove the static modifier the code block is an instance initializer.

What is meant by static initialization of objects?

You initialize a static object with a constant expression, or an expression that reduces to the address of a previously declared extern or static object, possibly modified by a constant expression.


2 Answers

Step 3 of the class initialization procedure, specified in JLS section 12.4.2, is

If the Class object for C indicates that initialization is in progress for C by the current thread, then this must be a recursive request for initialization. Release LC and complete normally.

Creating an instance of a class in its static initializer doesn't recursively reinitialize the class; recursive requests for initialization are detected and don't do anything.

(Note that "complete normally" means "the operation is done", not "follow the steps that would normally be followed to complete the operation"; it's opposite to "complete abruptly", which would mean we got some kind of exception or error.)

like image 163
user2357112 supports Monica Avatar answered Oct 15 '22 17:10

user2357112 supports Monica


The static initializer only gets called when the class is loaded. Instantiating an object in the static initializer isn't going to result in the class getting loaded again, classloading of a class only happens once, the classloader caches the class for the life of the JVM instance.

If you created a new instance in the instance initializer or in the constructor then you'd get the stackoverflow error.

like image 25
Nathan Hughes Avatar answered Oct 15 '22 17:10

Nathan Hughes