What will be the output of the following code?
class A {
static {
System.out.println("Inside static block A");
}
{
System.out.println("Inside init block A");
}
}
class B extends A {
static {
System.out.println("Inside static block B");
}
{
System.out.println("Inside init block B");
}
}
public class Test {
public static void main(String[] args) {
A b = new B();
}
}
Answer:
Inside static block A
Inside static block B
Inside init block A
Inside init block B
In Java, static blocks are executed first, followed by instance initializers.
In inheritance, the parent class is initialized before the child class.
Remember, there are restrictions on using the super keyword inside the child class constructor.
Comments
Post a Comment