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





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

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试