java – 关于classloader如何加载静态变量的说明

好吧,这是一个关于 java的新手问题,但是我似乎并没有把握头脑.

我的课程中有以下代码

private static final String [] LIST_CODE = gerarListCode();
private static final int [][] LIST_INTEGER = new int [][] {
        {947,947},{110,103},{947,958},120},954},{103,107},967},99,104}};

 private static String [] gerarListCode()
    {
        String [] listCode = new String [LIST_INTEGER.length];

        for (int i=0 ; i<LIST_INTEGER.length ; i++)
        {
           //do some stuff      
        }

        return listaUnicode;
    }

代码由于在以下行中的nullpointerexception而给我一个初始化异常

String [] listCode = new String [LIST_INTEGER.length];

此时变量LIST_INTEGER为空.

有人可以解释为什么吗是类加载器进程的线性,换句话说,它是否在完全加载所有其他变量之前调用方法

解决方法

是的,简而言之,它是线性的.

“What the compiler actually does is to
internally produce a single class
initialization routine that combines
all the static variable initializers
and all of the static initializer
blocks of code,in the order that they
appear in the class declaration. This
single initialization procedure is run
automatically,one time only,when the
class is first loaded.”

简单来说,从Java开始.

http://www.developer.com/java/other/article.php/2238491

您应该定义变量,然后以正确的顺序在静态intitializer块中初始化它们,或者您可以如下交换语句的顺序:

private static final int [][] LIST_INTEGER = new int [][] { {947,104}};

private static final String [] LIST_CODE = gerarListCode();

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...