If you execute this program you will get only the i value but not SIB, my question is when class loading into the memory SIB should execute and should give ooutput, but here I am getting only the i value? Then keep one method in class test then call that method from another class then you will get output of SIB, i method ( keep method also as static final)
class Test
{
static final int i =3;
static
{
System.out.println("SIB");
}
{
System.out.println("IIB");
}
}
class A1
{
public static void main(String[] args)
{
System.out.println(Test.i);
}
}
A
static finalvariable is a compile-time constant and its value is copied into the other class referencing it. Therefore your classTestwon’t load and no initializers will be executed. When the variable is onlystatic, then the class must be loaded to read the current value and your SIB block will be executed. The IIB block will be executed only when you instantiateTestwithnew Test().