deepak sharma
deepak sharma

Reputation: 29

Is it possible to create object in a static block?

I am little bit confused about static block.if we talk about system.out.println method here system is a class,out is the reference variable that have the reference ID of printstream class that declares in a static block then how it is possible to create any object in a static block because a static block always executes at class loading time, while object is created at run time... how i can make the difference b/w loading time and running time..

Upvotes: 2

Views: 3871

Answers (1)

Federico Piazza
Federico Piazza

Reputation: 30995

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.

For instance, with static initializers you can initialize a map with db data to be used later during object instantiation.

You can read this link that explains very well about it.

I find useful this quotation:

Static blocks are also called Static initialization blocks. A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword. Here is an example:

static {
    // whatever code is needed for initialization goes here
}

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code. And dont forget, this code will be executed when JVM loads the class. JVM combines all these blocks into one single static block and then executes.

For example, this code:

public class StaticExample{
    static {
        System.out.println("This is first static block");
    }

    public StaticExample(){
        System.out.println("This is constructor");
    }

    public static String staticString = "Static Variable";

    static {
        System.out.println("This is second static block and "
                                                + staticString);
    }

    public static void main(String[] args){
        StaticExample statEx = new StaticExample();
        StaticExample.staticMethod2();
    }

    static {
        staticMethod();
        System.out.println("This is third static block");
    }

    public static void staticMethod() {
        System.out.println("This is static method");
    }

    public static void staticMethod2() {
        System.out.println("This is static method2");
    }
}

Generates this output:

This is first static block
This is second static block and Static Variable
This is static method
This is third static block
This is constructor
This is static method2

Static method (alternative to static block)

You can write a private static method to achieve the same, and an advantage of using private static methods is that they can be reused later if you need to reinitialize the class variable.

For instance:

class Whatever {
    public static varType myVar = initializeClassVariable();

    private static varType initializeClassVariable() {

        // initialization code goes here
    }
}

Upvotes: 2

Related Questions