java 代码块

    xiaoxiao2021-12-14  47

    静态代码块

    特点:

    随着类的加载而执行,并且只会执行一次。

    作用

    用于给类进行初始化

    用法

    class StaticCode{ static int num; static { num = 2222; System.out.println("执行静态代码块"); } static void show() { System.out.println(num); } } class StaticDemo { public static void main(String[] args) { StaticCode.show(); StaticCode.show(); } } 执行结果 执行静态代码块 2222 2222
    转载请注明原文地址: https://ju.6miu.com/read-964798.html

    最新回复(0)