栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 静态代码块、非静态代码块、构造方法 执行顺序

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 静态代码块、非静态代码块、构造方法 执行顺序

1.静态(static)代码块

static修饰的成员都是类成员,会随着JVM加载类的时候加载而执行,而没有被static修饰的成员也被称为实例成员,需要创建对象才会随之加载到堆内存。所以静态的会优先非静态的。

2.非静态代码块

非静态代码块和静态代码块都是在JVM加载类时且在构造方法执行之前执行,非静态代码块在类中都可以定义多个;非静态代码块是在类被实例化的时候执行。每被实例化一次,就会被执行一次;

3.构造方法

执行构造方法的时候,在执行方法体之前存在隐式三步:
1)构造方法体的第一行是this语句,则不会执行隐式三步,
2)构造方法体的第一行是super语句,则调用相应的父类的构造方法,
3)构造方法体的第一行既不是this语句也不是super语句,则隐式调用super(),即其父类的默认构造方法,这也是为什么一个父类通常要提供默认构造方法的原因;

代码示例:
package com.dhcc.demo.springbootymldemo02.test;
import java.sql.Timestamp;
public class Person {
    private Integer no;
    private Integer age;
    private String name;
    static {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        try {
            Thread.sleep(1000);
            System.out.println("------父类的静态代码块执行------" + timestamp);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
     {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        try {
            Thread.sleep(1000);
            System.out.println("------父类的非静态代码块执行------" + timestamp);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    Person() {
         Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        try {
            Thread.sleep(1000);
            System.out.println("------父类的构造方法执行------" + timestamp);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
package com.dhcc.demo.springbootymldemo02.test;
import java.sql.Timestamp;
public class Student extends Person {
    private Integer sNo;
    private Integer classNO;
    static {
         Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        try {
            Thread.sleep(1000);
            System.out.println("------Student类的静态代码块执行------" + timestamp);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        try {
            Thread.sleep(1000);
            System.out.println("------Student类的非静态代码块执行------" + timestamp);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public Student() {
        super();
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        try {
            Thread.sleep(1000);
            System.out.println("------Student类的构造方法执行------" + timestamp);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

package com.dhcc.demo.springbootymldemo02.test;
public class Test {
    public static void main(String[] args) {
        new Student();
    }
}
------父类的静态代码块执行------2022-03-14 17:40:45.101
------Student类的静态代码块执行------2022-03-14 17:40:46.111
------父类的非静态代码块执行------2022-03-14 17:40:47.113
------父类的构造方法执行------2022-03-14 17:40:48.12
------Student类的非静态代码块执行------2022-03-14 17:40:49.132
------Student类的构造方法执行------2022-03-14 17:40:50.1
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/762891.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号