静态代码块、静态变量,构造代码块、实例变量的执行顺序

静态代码块、静态变量,构造代码块、实例变量的执行顺序

1、父类和子类有同名同类型的属性时

public class Test {
    public static void main(String[] args) {
        // 使用多态
        Parent chidParent = new Child();
        System.out.println("Parent:" + chidParent.getAge()); //40
        System.out.println("Parent:" + chidParent.age); //18 这个结果你能接受吗?哈哈

        // 直接使用原本类型
        Child child = new Child();
        System.out.println("Child:" + child.getAge()); //40
        System.out.println("Child:" + child.age); //40
    }
}

@Getter
@Setter
class Child extends Parent {
    public Integer age = 40;
}

@Getter
@Setter
class Parent {
    public Integer age = 18;
}
  1. 属性属于实例自己的,所以Parent的age属性值是18,这就解释通了

  2. 属性不存在覆盖(即使同名),而方法是实实在在的覆盖(复写)。所以你调用getAge()方法返回的100%是40喽

    2、

    public class Test {
        public static void main(String[] args) {
            new Child();
        }
    }
    
    @Getter
    class Child extends Parent {
        static {
            System.out.println("Child的静态块");
        }
        {
            System.out.println("Child的构造块");
        }
        Child() {
            System.out.println("Child的构造方法");
        }
    }
    
    @Getter
    class Parent {
        Integer age = 18;
        static {
            System.out.println("Parent的静态块");
        }
        {
            System.out.println("Parent的构造块");
        }
        Parent() {
            System.out.println("Parent的构造方法");
        }
    }
    

    结果输出:

    Parent的静态块

    Child的静态块

    Parent的构造块

    Parent的构造方法

    Child的构造块

    Child的构造方法

如果子类的构造器没有显示地调用父类的构造器,则将自动调用父类默认(没有参数) 的构造器

3、

public class StaticTest {

    public static void main(String[] args) {
        staticFunction();
    }

    // 静态变量(有实例化的过程,这就是本题的重点)
    static StaticTest st = new StaticTest();
    static {
        //System.out.println(b); // 编译报错:因为b在构造代码块后边,此处不能引用。因此Java代码是从上到下的顺序
        System.out.println("1");
    }
    {
        System.out.println("2");
    }
    StaticTest() {
        System.out.println("3");
        System.out.println("a=" + a + ",b=" + b);
    }
    public static void staticFunction() {
        System.out.println("4");
    }

    // 这两个变量写在最后面
    int a = 110;
    static int b = 112;
}

在这里插入图片描述

2
3
a=110,b=0
1
4

  1. 先初始化静态变量,也就是执行new StaticTest(),从而打印:2

  2. 再执行构造函数,打印:3和

    a=110,b=0
    
    1. 为何a=110,而b却为0呢?
      1. 执行构造函数之前,必须初始化实例属性,所以a=110
      2. 静态变量从上到下初始化,而st变量还没初始化完呢,所以b此时值为0
  3. 执行紧跟着的静态代码块。打印:1

  4. 执行静态方法staticFunction,打印:4

4、静态代码块属于类的,并且优先于main方法执行

public class StaticDemo1 {

    public static void main(String[] args) {
        speak();
        //StaticDemo1 t1 = new StaticDemo1();
        //System.out.println(t1.i);

    }
    // 静态变量
    static int i = 1;
    // 静态方法
    static void speak() {
        System.out.println("a");
    }
    // 静态代码块
    static {
        i = i + 3;
        System.out.println(i);
    }

    // 构造函数
    public StaticDemo1() {
        i = i + 5;
        System.out.println(i);
    }
}

4

a

5、

public class StaticDemo1 {

    public static void main(String[] args) {
        StaticDemo1 t1 = new StaticDemo1();
        System.out.println(t1.i);
        speak();

    }
    // 静态变量
    static int i = 1;
    // 静态方法
    static void speak() {
        System.out.println("a");
    }
    // 静态代码块
    static {
        i = i + 3;
        System.out.println(i);
    }

    // 构造函数
    public StaticDemo1() {
        i = i + 5;
        System.out.println(i);
    }
}

4

9

9
a

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值