I am studying the static block function in the Java kernel.
public class ClassResolution { static class Parent { public static String name = "Sparsh"; static { System.out.println("this is Parent"); name = "Parent"; } } static class Child extends Parent { static { System.out.println("this is Child"); name = "Child"; } } public static void main(String[] args) throws ClassNotFoundException { System.out.println(Child.name); } }
I thought the output would be:
this is Parent this is Child Child
but the actual conclusion is:
this is Parent Parent
and I have no idea why.
java
sparsh610
source share