Java中代码执行顺序

1. 名词解释:

非静态代码块:直接由{ }包裹的代码。

静态代码块:直接由static{ }包裹起来的代码。

形参和实参:传入所定义函数中的未赋值参数为形参,赋值了的即为实参。

成员变量:类中定义的变量即为成员变量,分为静态变量和非静态变量

  • 静态变量又称类变量,是用static修饰的成员变量
  • 非静态变量又称实例变量,没有用static修饰

2.执行顺序:

  • 首先执行静态区域(包括静态变量和静态代码块)部分,如果有父类先执行父类静态再执行子类静态。这是因为静态修饰部分是在创建实例对象之前就进行了初始化,而父类初始化又先与子类。
  • 然后执行非静态代码块,非静态变量和非静态代码块此时会被提到类的构造器中执行,且先与构造器中的代码,而父类构造器先与子类构造器。

静态方法只允许直接访问静态成员,且静态区域先与实例对象被分配到堆中,故而静态方法中不能使用this关键字。

 3.代码实现

package com.test.reponsibility;

/**
 * @author : zhanghj
 */
class Parent {
    public int parentNum=0;
    public static int staticParentNum=0;

    {
        System.out.println("Parent---执行非静态代码块了1!");
    }

    {
        System.out.println("Parent---执行非静态代码块了2!");
    }

    static{
        System.out.println("Parent---执行静态代码块了1!");
    }

    static{
        System.out.println("Parent---执行静态代码块了2!");
    }

    public Parent(){
        System.out.println("Parent---无参构造函数!");
    }
    public Parent(int parentNum){
        this.parentNum=parentNum;
        System.out.println("Parent---有参构造函数!");

    }

    public void ParentMethod(int parentNum){
        this.parentNum=parentNum;
        System.out.println("Parent---非静态方法/parentNum="+parentNum);
    }

    public static void staticParentMethod(int staticParentNum){
        com.test.sequence.Parent.staticParentNum=staticParentNum;
        System.out.println("Parent---静态方法/staticParentNum="+staticParentNum);
    }

}

class Child extends com.test.sequence.Parent {

    public int childNum=0;
    public static int staticChildNum=0;

    {
        System.out.println("Child---执行非静态代码块了1!");
    }

    {
        System.out.println("Child---执行非静态代码块了2!");
    }

    static{
        System.out.println("Child---执行静态代码块了1!");
    }

    static{
        System.out.println("Child---执行静态代码块了2!");
    }

    public Child(){
        super();
        System.out.println("Child---无参构造函数!");
    }

    public Child(int childNum){
        super(childNum);
        System.out.println("Child---有参构造函数!");
    }

    public void childMethod(int childNum){
        this.childNum=childNum;
        System.out.println("Child--非静态方法/childNum="+childNum);
    }

    public static void staticChildMethod(int staticChildNum){
        com.test.sequence.Child.staticChildNum=staticChildNum;
        System.out.println("Child---静态方法/staticChildNum="+staticChildNum);
    }


}

public class Test01 {
    static{
        System.out.println("Test---静态代码块!");
    }
    public static void main(String[] args) {
        int key=1;
        switch (key) {
            case 0:
                com.test.sequence.Parent parent=new com.test.sequence.Parent();
                break;
//            Parent---执行静态代码块了1!
//            Parent---执行静态代码块了2!
//            Parent---执行非静态代码块了1!
//            Parent---执行非静态代码块了2!
//            Parent---无参构造函数!
//          说明:先加载静态代码块,后加载非静态代码块
            case 1:
                com.test.sequence.Child b= new com.test.sequence.Child();
                break;
//            Parent---执行静态代码块了1!
//            Parent---执行静态代码块了2!
//            Child---执行静态代码块了1!
//            Child---执行静态代码块了2!
//            Parent---执行非静态代码块了1!
//            Parent---执行非静态代码块了2!
//            Parent---无参构造函数!
//            Child---执行非静态代码块了1!
//            Child---执行非静态代码块了2!
//            Child---无参构造函数!
//            说明:创建子类,会先执行父类,先执行父类静态——>子类静态——>父类非静态——>父类构造
//——>子类非静态——>子类构造
            case 2:
                com.test.sequence.Child c= new com.test.sequence.Child(4);
                //这个构造函数中指明了调用父类的有参构造函数,若不指定,则调用父类无参构造函数
                break;
//            Parent---执行静态代码块了1!
//            Parent---执行静态代码块了2!
//            Child---执行静态代码块了1!
//            Child---执行静态代码块了2!
//            Parent---执行非静态代码块了1!
//            Parent---执行非静态代码块了2!
//            Parent---有参构造函数!
//            Child---执行非静态代码块了1!
//            Child---执行非静态代码块了2!
//            Child---有参构造函数!
            //说明:静态代码块或非静态代码块执行顺序,按照代码前后编写顺序。
            case 3:
                com.test.sequence.Child d= new com.test.sequence.Child();
                com.test.sequence.Child e= new com.test.sequence.Child(4);
                break;
//            Parent---执行静态代码块了1!
//            Parent---执行静态代码块了2!
//            Child---执行静态代码块了1!
//            Child---执行静态代码块了2!
//            Parent---执行非静态代码块了1!
//            Parent---执行非静态代码块了2!
//            Parent---无参构造函数!
//            Child---执行非静态代码块了1!
//            Child---执行非静态代码块了2!
//            Child---无参构造函数!
//            Parent---执行非静态代码块了1!
//            Parent---执行非静态代码块了2!
//            Parent---有参构造函数!
//            Child---执行非静态代码块了1!
//            Child---执行非静态代码块了2!
//            Child---有参构造函数!
            //说明:创建多个子类,但父类静态代码块只执行一次。
            case 4:
                com.test.sequence.Child.staticChildMethod(4);
                break;
//            Parent---执行静态代码块了1!
//            Parent---执行静态代码块了2!
//            Child---执行静态代码块了1!
//            Child---执行静态代码块了2!
//            Child---静态方法/staticChildNum=4
            //说明:静态方法只可以调用静态变量。
            case 5:
                com.test.sequence.Parent.staticParentMethod(5);
                break;
//            Parent---执行静态代码块了1!
//            Parent---执行静态代码块了2!
//            Parent---静态方法/staticParentNum=5
            //说明:静态方法可通过 父类名.静态方法() 调用。
            case 6:
                System.out.println("父类的静态变量值staticParentNum="+ com.test.sequence.Parent.staticParentNum);
                break;
//            Parent---执行静态代码块了1!
//            Parent---执行静态代码块了2!
//            父类的静态变量值staticParentNum=0
            //说明:调用静态变量时,静态代码块会执行。
            case 7:
                System.out.println("子类的静态变量值staticChildNum="+ com.test.sequence.Child.staticChildNum);
                break;
//            Parent---执行静态代码块了1!
//            Parent---执行静态代码块了2!
//            Child---执行静态代码块了1!
//            Child---执行静态代码块了2!
//            子类的静态变量值staticChildNum=0
            //说明:调用子类静态变量,父类静态代码块和子类静态代码块会被执行。
            case 8:
                System.out.println("父类的静态变量值staticParentNum="+ com.test.sequence.Parent.staticParentNum);
                System.out.println("子类的静态变量值staticChildNum="+ com.test.sequence.Child.staticChildNum);
                break;
//            Parent---执行静态代码块了1!
//            Parent---执行静态代码块了2!
//            父类的静态变量值staticParentNum=0
//            Child---执行静态代码块了1!
//            Child---执行静态代码块了2!
//            子类的静态变量值staticChildNum=0

            case 9:
                com.test.sequence.Child f= new com.test.sequence.Child();
                f.ParentMethod(3);
                break;
//            Parent---执行静态代码块了1!
//            Parent---执行静态代码块了2!
//            Child---执行静态代码块了1!
//            Child---执行静态代码块了2!
//            Parent---执行非静态代码块了1!
//            Parent---执行非静态代码块了2!
//            Parent---无参构造函数!
//            Child---执行非静态代码块了1!
//            Child---执行非静态代码块了2!
//            Child---无参构造函数!
//            Parent---非静态方法/parentNum=3
            //说明:创建子类,用子类调用父类方法,非静态方法可以调用静态变量。

            default:
                break;
        }

    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值