Java(静态)变量、(静态)代码块、构造方法的执行顺序

Java(静态)变量、(静态)代码块、构造方法的执行顺序

总结 1、父类静态变量和静态代码块(先声明的先执行); 2、子类静态变量和静态代码块(先声明的先执行); 3、父类的变量和代码块(先声明的先执行); 4、父类的构造方法; 5、子类的变量和代码块(先声明的先执行); 6、子类的构造方法。 注意事项 1、非静态代码块于构造方法之前执行,并且 每次实例化对象之前都会先执行非静态代码块,静态代码块于非静态代码块之前执行; 2、在类第一次被调用时,加载该类, 静态代码块只执行一次; 3、静态代码块只能调用静态变量,静态方法只能调用静态变量; 4、非静态代码块或非静态方法可以调用任何(静态+非静态)变量; 5、非静态代码块在实例化对象时,于构造方法之前执行。

参考代码 1

class HelloA {
    public HelloA() {
        System.out.println("HelloA");
    }

    {
        System.out.println("I'm A class");
    }

    static {
        System.out.println("static A");
    }
}

public class HelloB extends HelloA {
    public HelloB() {
        System.out.println("HelloB");
    }

    {
        System.out.println("I'm B class");
    }

    static {
        System.out.println("static B");
    }

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

运行结果

static A
static B
I'm A class
HelloA
I'm B class
HelloB

参考代码 2

class A {
    public A() {
        System.out.println("HelloA");
    }

    {
        System.out.println("I'm A class");
    }

    static {
        System.out.println("static A");
    }
}

public class B extends A {
    public B() {
        System.out.println("HelloB");
    }

    {
        System.out.println("I'm B class");
    }

    static {
        System.out.println("static B");
    }

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

运行结果

static A
static B
I'm A class
HelloA
I'm B class
HelloB

参考代码 3

class A {
    static {
        System.out.print("1");
    }

    public A() {
        System.out.print("2");
    }
}

class B extends A {
    static {
        System.out.print("a");
    }

    public B() {
        System.out.print("b");
    }
}

public class Hello {
    public static void main(String[] args) {
        new B();
        new B();
    }
}

运行结果

1a2b2b

参考代码 4

public class HelloA {
    static {
        System.out.println("static A");
    }

    {
        System.out.println("I'm A class");
    }

    public HelloA() {
        System.out.println("HelloA");
    }

    public HelloA(String s) {
        System.out.println(s + "HelloA");
    }

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

class HelloB extends HelloA {
    public HelloB() {
        // 只能调用一次父类的构造方法,如果不写,Java 编译器会自动调用无参的构造方法。
        // super();
        super("parent");
        System.out.println("HelloB");
    }

    {
        System.out.println("I'm B class");
    }

    static {
        System.out.println("static B");
    }
}

运行结果

static A
static B
I'm A class
parentHelloA
I'm B class
HelloB

参考代码 5

public class People {
    String name;

    public People() {
        System.out.println(1);
    }

    public People(String name) {
        System.out.println(2);
        this.name = name;
    }
}

class Child extends People {
    People father;

    public Child() {
        // super(); // 系统默认调用父类的无参构造方法
        System.out.println(4);
    }

    public Child(String name) {
        // super(); // 系统默认调用父类的无参构造方法
        System.out.println(3);
        this.name = name;
        father = new People(name + ":F");
        System.out.println(father.name);
    }

    public static void main(String[] args) {
        new Child();
        // new Child("mike");
    }
}

运行结果

1
4

参考代码 6

public class People {
    String name;

    public People() {
        System.out.println(1);
    }

    public People(String name) {
        System.out.println(2);
        this.name = name;
    }
}

class Child extends People {
    People father;

    public Child() {
        // super(); // 系统默认调用父类的无参构造方法
        System.out.println(4);
    }

    public Child(String name) {
        // super(); // 系统默认调用父类的无参构造方法
        System.out.println(3);
        this.name = name;
        father = new People(name + ":F");
        System.out.println(father.name);
    }

    public static void main(String[] args) {
        // new Child();
        new Child("mike");
    }
}

运行结果

1
3
2
mike:F

参考代码 7

public class ExA {
    private static ExA a = new ExA();

    static {
        System.out.println("父类---静态代码块");
    }

    public ExA() {
        System.out.println("父类---构造方法");
    }

    {
        System.out.println("父类---非静态代码块");
    }

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

class ExB extends ExA {
    private static ExB b = new ExB();

    static {
        System.out.println("子类---静态代码块");
    }

    {
        System.out.println("子类---非静态代码块");
    }

    public ExB() {
        System.out.println("子类---构造方法");
    }
}

运行结果

父类---非静态代码块
父类---构造方法
父类---静态代码块
父类---非静态代码块
父类---构造方法
子类---非静态代码块
子类---构造方法
子类---静态代码块
父类---非静态代码块
父类---构造方法
子类---非静态代码块
子类---构造方法

参考代码 8

class Foo {
    public Foo(String word) {
        System.out.println(word);
    }
}

class Parent {
    static Foo FOO = new Foo("Parent's static parameter");

    Foo foo = new Foo("Parent's parameter");

    static {
        System.out.println("Parent's static code block");
    }

    {
        System.out.println("Parent's code block");
    }

    public Parent() {
        System.out.println("Parent.Parent()");
    }
}

public class Child extends Parent {
    static Foo FOO = new Foo("Child's static parameter");

    Foo foo = new Foo("Child's parameter");

    static {
        System.out.println("Child's static code block");
    }

    {
        System.out.println("Child's code block");
    }

    public Child() {
        System.out.println("Child.Child()");
    }

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

运行结果

Parent's static parameter
Parent's static code block
Child's static parameter
Child's static code block
Parent's parameter
Parent's code block
Parent.Parent()
Child's parameter
Child's code block
Child.Child()

参考代码 9

public class GFG {
    public GFG(int x) {
        System.out.println("ONE argument constructor");
    }

    public GFG() {
        System.out.println("No argument constructor");
    }

    static {
        System.out.println("1st static init");
    }

    {
        System.out.println("1st instance init");
    }

    {
        System.out.println("2nd instance init");
    }

    static {
        System.out.println("2nd static init");
    }

    public static void main(String[] args) {
        new GFG();
        new GFG(8);
    }
}

运行结果

1st static init
2nd static init
1st instance init
2nd instance init
No argument constructor
1st instance init
2nd instance init
ONE argument constructor

参考代码 10

class MyTest {
    static {
        initialize();
    }

    private static int sum;

    public static int getSum() {
        initialize();
        return sum;
    }

    private static boolean initialized = false;

    private static void initialize() {
        if (!initialized) {
            for (int i = 0; i < 100; i++)
                sum += i;
            initialized = true;
        }
    }
}

public class GFG {
    public static void main(String[] args) {
        System.out.println(MyTest.getSum());
    }
}

运行结果

9900

参考代码 11

class MyTest {
    static {
        initialize();
    }

    private static int sum = 0;

    public static int getSum() {
        initialize();
        return sum;
    }

    private static boolean initialized = false;

    private static void initialize() {
        if (!initialized) {
            for (int i = 0; i < 100; i++)
                sum += i;
            initialized = true;
        }
    }
}

public class GFG {
    public static void main(String[] args) {
        System.out.println(MyTest.getSum());
    }
}

运行结果

4950

参考代码 12

class MyTest {
    static {
        initialize();
    }

    private static int sum = 0;

    public static int getSum() {
        initialize();
        return sum;
    }

    private static boolean initialized;

    private static void initialize() {
        if (!initialized) {
            for (int i = 0; i < 100; i++)
                sum += i;
            initialized = true;
        }
    }
}

public class GFG {
    public static void main(String[] args) {
        System.out.println(MyTest.getSum());
    }
}

运行结果

0

参考代码 13

class MyTest {
    static {
        initialize();
    }

    private static int sum;

    public static int getSum() {
        initialize();
        return sum;
    }

    private static boolean initialized;

    private static void initialize() {
        if (!initialized) {
            for (int i = 0; i < 100; i++)
                sum += i;
            initialized = true;
        }
    }
}

public class GFG {
    public static void main(String[] args) {
        System.out.println(MyTest.getSum());
    }
}

运行结果

4950

10-13 题解析

参考资料

转载于:https://www.cnblogs.com/hglibin/p/9996816.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值