Static vs Instance Initialization Block

Static Initialization BlockInstance Initialization Block
To define a static initialization block we use the keyword staticNo keyword is required to define an instance initialization block.
A static initialization block loads as soon as a class loads and it is not associated with a call to the constructor of a class for object creation.An instance initialization block is only executed when there is a call to the constructor for creating an object.
Static block can only access static variables and static methods of its classAn instance initialization block can not only access static variables and static methods but also instance variables and instance methods of the class.
There is no automatic call to superclass constructor from the static initialization block.An instance initialization block always makes an automatic call to superclass constructor by calling super() before executing any other statement in it.
Static block is called just once during the entire execution of the program when the class loads.Instance initialization block can run many times, whenever there is a call to the constructor of the class
A class with a static and an instance initialization block.

Here we have a class A with both static and instance initialization block defined in it.

  • Static initialization block will be called as soon as the class A
    loads.
  • Instance initialization block will only be called when the
    constructor of the class is called for creating an object of class A.
class A
{
static char ch='a';
int a=10;

//Static initialization block of A
static
{
System.out.println("Static block runs");
System.out.println("value of static character = "+ ch);
}

//Instance initialization block of B
{
System.out.println("Instance Initialization block runs")
System.out.println("Value of static character = "+ ch);
System.out.println("Value of instance variable = "+ a);
}


public static void main(String... ar)
{
A ob= new A();
}

}

Output:

Static Initialization block runs
Value of static character = a
Instance Initialization block runs
Value of static character = a
Value of instance variable = 10

As we can see in the code, the static initialization block was called as soon as the class was loaded and it accessed static variable int. However, the instance initialization block ran only when there was a call to constructor of class A and it could access not only instance variable, a, but also static variable, ch.

Static and instance initialization block in two classes with inheritance
class B
{

    //Constructor of B
    B()
    {
        System.out.println("Constructor of B class is called");
    }

    //Instance initialization block of B
    {
        System.out.println("An object of B is being created");
    }

    //Static initialization block of B
    static
    {
        System.out.println("B class is loaded");
    }

}


class A extends B
{

    //Constructor of A
    A()
    {
        System.out.println("Constructor of class A is called");
    }

    //Static initialization block of A
    static
    {
        System.out.println("A class is loaded");
    }


    //Instance initialization block of A
    {
    //Compiler makes an automatic call to superclass constructor, by calling super() here.
        System.out.println("An object of A is being created");
    }


    public static void main(String... ar)
    {
        A ob= new A();
    }

}

Output

B class is loaded
A class is loaded
An object of B is being created
Constructor of B class is called
An object of A is being created
Constructor of class A is called
  • When the program executes, the static initialization block of
    superclass B is executed before the static initialization block of
    subclass A, because first a superclass loads and then its subclass is
    loaded.
  • Calling the constructor of class A automatically calls the
    superclass(class B) constructor by making an invisible all to
    super(). , hence -

First, instance initialization block of subclass B is called executed.
Second, superclass B constructor is called and executed.
Trird, instance initialization block of subclass A is called executed.
Finally, the constructor of subclass A finishes its execution.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值