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

前端之家收集整理的这篇文章主要介绍了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();

猜你在找的Java相关文章