Search code examples
javainheritancestatic-block

static block with inheritance


I am revisiting the concepts of java. So, I am looking in this example

 class A {
    A( ) {System.out.print("CA");}
    static {System.out.print("SA");}
}
class B extends  A {
    B() {System.out.print("CB");}
    static {System.out.print("SB");}
    public static void main(String[] args) {
        B b = new B();
    }
}

output here is SASBCACB

So I didn't understand why CA message is printed since class A constructor is not called.

My question is kind of basic java but its better to know things rather blank about it.


Solution

  • A is instantiated when you create a B, that is how inheritance works.