博客
关于我
java 中类的加载顺序
阅读量:111 次
发布时间:2019-02-26

本文共 1731 字,大约阅读时间需要 5 分钟。

 转载自:     

 

1、虚拟机在首次加载Java类时,会对静态初始化块、静态成员变量、静态方法进行一次初始化

2、只有在调用new方法时才会创建类的实例
3、类实例创建过程:按照父子继承关系进行初始化,首先执行父类的初始化块部分,然后是父类的构造方法;再执行本类继承的子类的初始化块,最后是子类的构造方法
4、类实例销毁时候,首先销毁子类部分,再销毁父类部分

复制代码

public class Parent{    public static int t = parentStaticMethod2();    {        System.out.println("父类非静态初始化块");    }    static    {        System.out.println("父类静态初始化块");    }    public Parent()    {        System.out.println("父类的构造方法");    }    public static int parentStaticMethod()    {        System.out.println("父类类的静态方法");        return 10;    }    public static int parentStaticMethod2()    {        System.out.println("父类的静态方法2");        return 9;    }      @Override    protected void finalize() throws Throwable    {        // TODO Auto-generated method stub        super.finalize();        System.out.println("销毁父类");    }    }

复制代码

复制代码

public class Child extends Parent{    {        System.out.println("子类非静态初始化块");    }    static    {        System.out.println("子类静态初始化块");    }    public Child()    {        System.out.println("子类的构造方法");    }    public static int childStaticMethod()    {        System.out.println("子类的静态方法");        return 1000;    }    @Override    protected void finalize() throws Throwable    {        // TODO Auto-generated method stub        super.finalize();        System.out.println("销毁子类");    }}

复制代码

复制代码

public class Test{       public static void main(String[] args)    {        // TODO Auto-generated method stub        Parent.parentStaticMethod();//        Child child = new Child();            }}

复制代码

输出

父类的静态方法2父类静态初始化块父类类的静态方法

类中static 方法在第一次调用时加载,类中static成员按在类中出现的顺序加载。当调用静态方法2时输出

父类的静态方法2父类静态初始化块父类的静态方法2

注释掉Parent.parentStaticMethod();

去掉注释Child child = new Child();

复制代码

父类的静态方法2父类静态初始化块子类静态初始化块父类非静态初始化块父类的构造方法子类非静态初始化块子类的构造方法
你可能感兴趣的文章
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>