Java变量初始化流程及其中的一些问题

个人学习笔记
好像标题有点问题,有一些不严谨之处,另外还有一些问题不太明白如果有人知道答案还请赐教,谢谢
文章参考:
1.luohuacanyue:http://blog.csdn.net/luohuacanyue/article/details/13169173
2.sophine:http://www.cnblogs.com/sophine/p/3531282.html
这两位的代码给了我很大启发,在此根据自己的理解做了一下小小的总结
注:输出顺序已经在代码中标记出了

class Code {
    {
        System.out.println("4.Code的构造代码块");
    }

    static {
        System.out.println("3.Code的静态代码块");
    }

    public Code() {
        System.out.println("5.Code的构造方法");
    }
}

public class CodeBlock03 {
    //public static  int a=1;//若放在这里static块中可以直接输出a,放在下面就不行了
   static {
        System.out.println("1.CodeBlock03的静态代码块");
        a=9;
        //Cannot reference a field before it is defined,引用a之前必须要定义它,这里不能引用只能赋值,但是可以通过类名.a来访问,或者把public static  int a=0放到访问之前也可以,不太理解原理。
        //System.out.println(a);
    }
    //这一句也是一个静态代码块,执行完“1CodeBlock03的静态代码块”后接着执行这一句,接着返回main方法执行,这里若不对a赋值那么a取最近一次赋值结果
    public static  int a=8;

    public CodeBlock03() {

        System.out.println("7.CodeBlock03的构造方法");
    }

    {
        System.out.println("6.CodeBlock03的构造代码块");
    }

    public static void main(String[] args) {
        System.out.println("2.CodeBlock03的主方法,a的值为"+a);
        new Code();
        new CodeBlock03();

    }
}

1.18.2016 14:07
前段时间遇到一种写法

public final static HashMap<Integer, String> ORDER_STATUS = new HashMap<Integer, String>() {
            {
                put(0, "a");
                put(9, "b");
                put(1, "c");
                put(2, "d");
                put(3, "e");
                put(4, "f");
                put(5, "g");
                put(6, "h");
                put(7, "i");           
          }
        };

在论坛上问了下各位高手,虽然当时看过大神们的回答,但是感觉还是怪怪的,今天再次看下突然发现这个不就是构造代码块吗?(构造代码块在创建对象时被调用,每次创建对象都会被调用,并且构造代码块的执行次序优先于类构造函数)于是恍然大悟。

该代码的详细解释:
http://www.coderanch.com/t/386333/java/java/HashMap-Construction-initial-values
感觉已经很清晰了
This expression creates an instance of an anonymous class which extends HashMap. The anonymous class includes an instance initializer block which contains the “put” calls. Are you familiar with either of these things yet? An anonymous inner class is a way of defining a subclass without giving it a name. An instance initializer block is basically a way to add code to all the constructors in a class. This code is really equivalent to

public class MyHashMap extends HashMap {
    public MyHashMap {
       put("key1","value1");
       put("key2","value2");
    }
}
...
public static final HashMap hm = new MyHashMap();

In any case, depending on who you ask, this is a cute hack, or a crime against humanity. It’s not a very common idiom, and I hope it doesn’t become more so.

小结:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值