risc-v中文社区

 找回密码
 立即注册
查看: 964|回复: 0

[原创] java for risc-v系列知识讲座(7)---类的静态代码块,静态成...

[复制链接]

20

主题

23

帖子

96

积分

注册会员

Rank: 2

积分
96
发表于 2021-8-3 21:21:58 | 显示全部楼层 |阅读模式
public class StaticTest {
    static String sa = "string sa";
    static String sb;
    String sc = "stirng sc";
    String sd;

    public StaticTest(String msg) {
        sd = msg;
        System.out.println("constructor running.....");
    }
    //类的静态代码块 在其它代码之前(包括构造函数)执行
    static {
        dispfun("static test before");
        sb = "string sb";
        dispfun("static test after");
    }
    static void dispfun(String msg) {
        System.out.println("--------" + msg + "-----------");
        System.out.println("sa=\"" + sa + "\"");
        System.out.println("sb=\"" + sb +"\"");
    }
    public static void main(String[] args)
    {
        StaticTest st = new StaticTest("hello"); //当静态代码块执行完之后就是构造函数执行
        System.out.println("st.sc = " + st.sc + ", st.sd = " + st.sd);
    }
}
显示如下:
--------static test before-----------
sa="string sa"
sb="null"   //静态代码块中sb是String的初值,String的初值默认是空串
--------static test after-----------
sa="string sa"
sb="string sb"
constructor running.....
st.sc = stirng sc, st.sd = hello


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



Archiver|手机版|小黑屋|risc-v中文社区

GMT+8, 2024-4-27 15:32 , Processed in 0.014961 second(s), 17 queries .

risc-v中文社区论坛 官方网站

Copyright © 2018-2021, risc-v open source

快速回复 返回顶部 返回列表