Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of how classloader loads static variables

Ok so this is a newbie question on java, but i can't seem to get my head around it.

I have the following code inside my class

private static final String [] LIST_CODE = gerarListCode();
private static final int [][] LIST_INTEGER = new int [][] {
        {947,947}, {110,103}, 
        {947,958}, {110,120}, 
        {947,954}, {103,107}, 
        {947,967}, {110,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;
    }

This code is giving me a initialization exception due to a nullpointerexception in the following line

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

Seems the variable LIST_INTEGER is null at that time.

Can someone explain why? is the classloader process linear, in other words, does it invoke the method before fully loading all the other variables?

like image 335
Nuno Furtado Avatar asked Jul 15 '09 19:07

Nuno Furtado


People also ask

How does a ClassLoader work internally?

Extension ClassLoader searches for the class in the Extension Classpath(JDK/JRE/LIB/EXT). If the class is available then it is loaded, if not the request is delegated to the Application ClassLoader. Application ClassLoader searches for the class in the Application Classpath.

How does a ClassLoader work in Java?

Class loaders are responsible for loading Java classes dynamically to the JVM (Java Virtual Machine) during runtime. They're also part of the JRE (Java Runtime Environment). Therefore, the JVM doesn't need to know about the underlying files or file systems in order to run Java programs thanks to class loaders.

When static methods are loaded in Java?

Static Block in Java It executes whenever the class is loaded in memory. One class can have numerous static blocks, which will be executed in the same sequence in which they are written.

How are static variables initialized in Java?

Static variables are initialized only once , at the start of the execution. These variables will be initialized first, before the initialization of any instance variables. A single copy to be shared by all instances of the class. A static variable can be accessed directly by the class name and doesn't need any object.


2 Answers

Yes, in short, it is linear.

"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."

Taken from Java in a nutshell.

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

You should define the variables and then initialize them in a static intitializer block in the correct order, or you could swap the order of the statements as follows:

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

private static final String [] LIST_CODE = gerarListCode(); 
like image 138
Jon Avatar answered Sep 21 '22 15:09

Jon


The JVM will, indeed, initialize the static fields in the order it encounters them.

A class's static fields are initialized when the class is first encountered by the JVM. According to Java Puzzlers, puzzle 49 (which goes on to reference JLS 4.12.5), static fields are first set to their default values. Object variables are set to null, ints are set to 0, etc. After that, their initializers are executed in order of appearance.

So, in your example, LIST_CODE and LIST_INTEGER are first set to null. Then, LIST_CODE is initialized by calling gerarListCode(). LIST_INTEGER is still null when that method is executed. Only after that, LIST_INTEGER is initialized with the literal value you give in your example.

like image 25
jqno Avatar answered Sep 22 '22 15:09

jqno