【java】Java内部类的执行顺序与继承中的执行顺序

本文详细解析了Java中外部类、内部类及静态内部类的初始化与执行流程,包括构造函数、实例代码块和静态代码块的执行顺序。同时,通过代码示例展示了继承关系中父类与子类的初始化过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上代码

public class Outer {
    private int age;
    private String name;
    public Outer(){
        System.out.println("Outer.init()");
    }
    {
        System.out.println("Outer.instance()");
    }
    static {
        System.out.println("Outer.static()");
    }
    public void func(){
        System.out.println(name+age);
    }
    class Inter{
        public static final int age2=10;
        private String name2;
        public Inter(){
            System.out.println("Inter.init()");
        }
        {
            System.out.println("Inter.instance()");
        }
        public void func(){
            System.out.println(name+age);
        }
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Outer.Inter inter=new Outer().new Inter();
    }
}

运行结果从运行结果我们可以看出,首先执行的是外部类的静态代码块,外部类实例代码块,外部类构造函数,然后是内部类实例代码块,内部类构造函数。最后还有内部类的成员方法,代码中并没有加入。需要注意的是,实例内部类也可以定义静态的成员属性,不过需要用final修饰,代表这个属性在编译时期就已经确定下来了,不能再修改。静态内部类大体和实例内部类相似,不过内部类要调用外部类成员,需要提供有外部类引用的构造函数。
代码示例:


public class Outer {
    private int age;
    private String name;
    public Outer(){
        System.out.println("Outer.init()");
    }
    {
        System.out.println("Outer.instance()");
    }
    static {
        System.out.println("Outer.static()");
    }
    public void func(){
        System.out.println("Outer.func()");
    }
    static class Inter{
        private static int age2=10;
        private String name2;
        private Outer outer;
        public Inter(Outer outer){
            this.outer=outer;
            System.out.println("Inter.init()");
        }
        public Inter(){
            System.out.println("Inter.init()");
        }
        public void func1(){
            outer.func();
            System.out.println("Inter.func()");
        }
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Outer outer=new Outer();
        Outer.Inter inter=new Outer.Inter(outer);
        inter.func1();
    }
}

然后是继承间的执行顺序

public class Parent {
    private int age;
    private String name;
    public Parent(){
        System.out.println("parent.init()");
    }
    {
        System.out.println("parent.instance");
    }
    static {
        System.out.println("parent.static");
    }
    public void func(){
        System.out.println("parent.func()");
    }
}
public class Son extends Parent{
    private String school;
    public Son(){
        System.out.println("Son.init()");
    }
    {
        System.out.println("Son.instance()");
    }
    static{
        System.out.println("Son.static");
    }
    public void func(){
        System.out.println("Son.func()");
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Son son=new Son();
        Parent parent=new Son();
        parent.func();
        son.func();
    }
}

运行结果

首先执行父类的静态代码块,然后子类静态代码块,接下来是父类属性已经构造方法,
最后是子类属性和构造方法。成员方法看调用顺序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值