static代码块

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wwwtotoro/article/details/79953465
package jd.www.Testjava;

class Parent{
    static String name = "hello";
    {
        System.out.println("parent block");
    }
    static {
        System.out.println("parent static block");
    }
    public Parent(){
        System.out.println("parent constructor");
    }
}
package jd.www.Testjava;

class Child extends Parent{
    static String childName = "hello";
    {
        System.out.println("child block");
    }
    static {
        System.out.println("child static block");
    }
    public Child(){
        System.out.println("child constructor");
    }

    public static void print(){
        System.out.print("hello");
    }
}
package jd.www.Testjava;

public class StaticIniBlockOrderTest {

    public static void main(String[] args) {
        Child.print();
    }
}
输出:
parent static block
child static block
hello

package jd.www.Testjava;

public class StaticIniBlockOrderTest {

    public static void main(String[] args) {
        new Child().print();
    }
}

输出:

parent static block
child static block
parent block
parent constructor
child block
child constructor
hello





阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页