Java类 初始化顺序 | 静态数据的初始化

public class StaticInit1 {
    StaticInit1(){
        System.out.println("StaticInit_1_init");
    }
    StaticInit1(int i){
        System.out.println("StaticInit_1_init,int i ="+i);
    }
}
public class StaticInit {
    StaticInit1 s2 = new StaticInit1(1);
    public StaticInit() {
        System.out.println("StaticInit-init");
    }
    static StaticInit1 s1 = new StaticInit1();
    public static void main(String[] args) {
        new StaticInit();
        /* output:
            StaticInit_1_init
            StaticInit_1_init,int i =1
            StaticInit-init
         */
    }
}

根据输出结果,可以发现
在初始化 StaticInit类时,先初始化了类的静态变量. 再是执行了变量s2的初始化,最后 才是类构造器的执行输出

对StaticInit类再加一个f()方法

public class StaticInit {
    StaticInit1 s2 = new StaticInit1(1);
    public StaticInit() {
        System.out.println("StaticInit-init");
    }
    static StaticInit1 s1 = new StaticInit1();
    void f(){
        new StaticInit();
    }
    public static void main(String[] args) {
        new StaticInit().f();
        /* output:
            StaticInit_1_init
            StaticInit_1_init,int i =1
            StaticInit-init
            StaticInit_1_init,int i =1
            StaticInit-init
         */
    }
}

查看输出结果 , 通过f()方法再次(第二次执行)创建Staticinit类时,初始化类时并未执行静态变量的初始化,也就是说 :静态变量的初始化只执行一次.


Next Example

public class Window {
    Window(int i){
        System.out.println("Window==i="+i);
    }
}
public class House {
    Window w1 = new Window(1);
    House(){
        System.out.println("house");
        w3 = new Window(33);
    }
    Window w2 = new Window(2);
    void walk(){
        System.out.println("walk");
    }
    Window w3 = new Window(3);
}
public class OrderOfInit {
    public static void main(String[] args) {
        House house = new House();
        house.walk();
    }
    /* Output:
            Window==i=1
            Window==i=2
            Window==i=3
            house
            Window==i=33
            walk
     */
}

静态数据初始化

public class Bowl {
    Bowl(int i) {
        print("Bowl->i="+i);
    }
    void f1(int i){
        print("f1->i="+i);
    }
}
public class Table {
    static Bowl b1 = new Bowl(1);
    Table(){
        print("Table");

    }
    Bowl b3 = new Bowl(3);
    void f2(int i){
        print("f2->i="+i);
    }
    static Bowl b2 = new Bowl(2);
}
public class Cupboard {
    Bowl b4 = new Bowl(4);
    static Bowl b5 = new Bowl(5);
    Cupboard(){
        print("cupboard");
        b4.f1(2);
    }

    void f3(int i){
        print("f3->i="+i);
    }
    static Bowl b6 = new Bowl(6);
}
public class StaticInit {
    public static void main(String[] args) {
        new Cupboard();
        new Cupboard();
        table.f2(1);
        cupboard.f3(1);
    }
    static Table table = new Table();
    static Table table1 = new Table();
    static Cupboard cupboard = new Cupboard();
    /*Output:								  产生输出对应的语句
        Bowl->i=1
        Bowl->i=2            static Table table = new Table();
        Bowl->i=3
        Table
        -----------
        Bowl->i=3            static Table table1 = new Table();
			 Table
			 -----------
        Bowl->i=5
        Bowl->i=6
        Bowl->i=4            static Cupboard cupboard = new Cupboard();
        cupboard
        f1->i=2
        -----------
        Bowl->i=4
        cupboard             new Cupboard();
        f1->i=2
        -----------
        Bowl->i=4
        cupboard              new Cupboard();
        f1->i=2
        -----------
        f2->i=1               table.f2(1);
        f3->i=1					    cupboard.f3(1);
     */
}

封装System.out.println输出语句

只能打印字符串…

public class Print {
    public static void print(String something){
        System.out.println(something);
    }
}

以静态方式引入

import static edu.Print.print;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦中千秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值