why static block in child class does not get executed?

前端 未结 5 724
难免孤独
难免孤独 2020-12-19 21:38

here is the code

public class ClassResolution {
static class Parent {
    public static String name;
    static {
        System.out.println(\"this is Parent         


        
5条回答
  •  醉话见心
    2020-12-19 22:12

    JLS#12.4.1. When Initialization Occurs

    A reference to a static field (§8.3.1.1) causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface.

    I guess above says it all..

提交回复
热议问题