Sequence in which Non Static Members get loaded in Java
Hi All,
I was brushing up my Java concept and faced a scenario and bit confused about it.
I made 2 classes ClassB and ClassA extends ClassB.
Below is the code
Code:
public class ClassB {
ClassB() {
System.out.println("Constructor Class B");
}
{
System.out.println("IIB class B");
}
static {
System.out.println("SIB Class B");
}
}
public class ClassA extends ClassB {
ClassA() {
System.out.println("Constructor Class A");
}
{
System.out.println("IIB class A");
}
static {
System.out.println("SIB Class A");
}
public static void main(String[] args) {
new ClassA();
}
}
OUPUT
SIB Class B
SIB Class A
IIB class B
Constructor Class B
IIB class A
Constructor Class A
I understand how the Static Initialization block works, but confused about the non static one.
According to my understanding, when the object gets created IIB of Parent then IIB of Child should print then the constructor.
Output according to my understanding:
SIB Class B
SIB Class A
IIB class B
IIB class A
Constructor Class B
Constructor Class A
Can anyone spot some more light on this
Re: Sequence in which Non Static Members get loaded in Java
The code would be easier to read if it were properly formatted and wrapped in code tags.
Code without formatting and indentations is hard to read and understand.