Explanation of how classloader loads static variables - java

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?

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();

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.

Related

Why an instance variable can be assigned and not be referenced before its declaration in Java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java static class initialization
in what order are static blocks and static variables in a class executed?
When I run this code the answer is 1, I thought it would be 2.
What is the order of initialization and the value of k in each step?
public class Test {
static {k = 2;}
static int k = 1;
public static void main(String[] args) {
System.out.println(k);
}
}
Edit 1: As a follow up to "k is set to default value" then why this next code doesn't compile? Theres an error "Cannot reference a field before it's defined".
public class Test {
static {System.out.println(k);}
static int k=1;
public static void main(String[] args) {
System.out.println(k);
}
}
Edit 2: For some unknow to me reason it^ works when instead of "k" its "Test.k".
Thanks for all the answers. this will sufice :D
They are executed in the order that you write them. If the code is:
public class Test {
static int k = 1;
static {k = 2;}
public static void main(String[] args) {
System.out.println(k);
}
}
then the output becomes 2.
The order of initialization is: ..the class variable initializers and static initializers of the class..., in textual order, as though they were a single block.
And the values (for your code) are: k = 0 (default), then it's set to 2, then it's set back to 1.
You can check that it's actually set to 2 by running the following code:
private static class Test {
static {
System.out.println(Test.k);
k = 2;
System.out.println(Test.k);
}
static int k = 1;
public static void main(String[] args) {
System.out.println(k);
}
}
Short answer
When the initialization of the class starts, k will have initial value of 0.
The static block (since it precedes the assignment in the declaration) is then executed, and k will be assigned 2.
Then the initializer in the declaration is executed, and k will be assigned 1.
Long explanation
Let us use this example, since your example is a bit simple:
class TestInitOrder {
static {
System.out.println(TestInitOrder.stat1);
System.out.println(TestInitOrder.stat2);
System.out.println(TestInitOrder.str);
System.out.println(TestInitOrder.str2);
str = "something";
System.out.println(TestInitOrder.str);
System.out.println(TestInitOrder.str2);
System.out.println(TestInitOrder.lazy);
System.out.println(TestInitOrder.second);
}
private static final int stat1 = 10;
static final String str2 = "sdfff";
static String str = "crap";
private static int stat2 = 19;
static final Second second = new Second();
static final int lazy;
static {
lazy = 20;
}
static {
System.out.println(TestInitOrder.str2);
System.out.println(TestInitOrder.stat2);
System.out.println(TestInitOrder.str);
System.out.println(TestInitOrder.lazy);
System.out.println(TestInitOrder.second);
}
public static void main(String args[]) {
}
}
class Second {
public Second() {
System.out.println(TestInitOrder.second);
}
}
According to Java Language Specification, from section 4.12.5:
Every variable in a program must have a value before its value is used:
Each class variable, instance variable, or array component is initialized with a default value when it is created
(The following lines from the specification specify the default value for all the types, basically some form of 0, such as 0, 0.0d, null, false, etc.)
So before the class is initialized (due to one of these reasons), the variables will hold an initial value.
According to the detailed initialization procedure (only the interesting steps are quoted here, and emphasis mine):
6.
[...] Then, initialize the final class variables and fields of interfaces whose values are compile-time constant expressions (§8.3.2.1, §9.3.1, §13.4.9, §15.28).
[...]
9.
Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.
Let us look at step 6, with the 4 final class variables: stat1, str2, second, lazy.
Since 10 is constant expression, and so is "sdfff", and due to the order of execution, it is not possible to observe the initial value for str2 and stat1. In order to make an observation, the earliest you can do is in step 9.
The case of second demonstrate that when the right hand side is not a compile-time constant expression, so its initial value is visible.
The case of lazy is different, since the assignment is done in static block, and hence happen in step 9 - so it is possible to observe its initial value. (Well, the compiler checks carefully that lazy is assigned exactly once).
After the initialization of final class variables with compile-time constant expression comes the execution of static blocks and the rest of the initializers.
As you can see from the example, the static blocks and initialization happens according to textual order - demonstrated with the use of str variable - it is first printed out as null, then something, then crap.

Why can't we set the value of static final variable in static block through class name [duplicate]

This question already has answers here:
Why isn't a qualified static final variable allowed in a static initialization block?
(2 answers)
Closed 8 years ago.
For example, consider code snap below:
public static final int a;
public static final int b;
static {
a = 8; // it's working
Test.b = 10; // compilation error Test.b cannot be assigned.
}
Why can't we use Test.b = 10; inside a static block of the Test class itself? Without the class name it's working fine.
Is there any reason behind this?
A static final variable must be initialized before use. It may be initialized either directly at declaration time, or in a static block.
But when you use class.var = x it is not seen as an initialization but as an assignation.
With a JDK 7, the error is cannot assign a value to final variable.
That explains why it works if you remove the final keyword
class Test {
static final int a = 2; // initialization at declaration time
static final int b;
static final int c;
static {
b = 4; // initialization in static block
Test.c = 6; // error : cannot assign a value to final variable c
}
...
}
EDIT
In the JLS the correct word for initialization is definite assignement
Extract from the JLS :
For every access of a local variable or blank final field x, x must be definitely
assigned before the access, or a compile-time error occurs.
Similarly, every blank final variable must be assigned at most once; it must be
definitely unassigned when an assignment to it occurs.
Such an assignment is defined to occur if and only if either the simple name of the
variable (or, for a field, its simple name qualified by this) occurs on the left hand
side of an assignment operator.
For every assignment to a blank final variable, the variable must be definitely
unassigned before the assignment, or a compile-time error occurs.
emphasize mine, but I think this is the real reason for the error.

Initializing static variables in java

What exactly is meant by "Static variables are initialized when a class is loaded"? I read lots of discussions available on the net but still I am confused. Step 2 is be the initialization step, right? Then what happens in step 1 "when the class is loaded"?
public class NewClass {
static int[] arr; //Step 1
NewClass(){
arr = new int[10]; //Step 2
for(int i= 0;i<10;i++){
arr[i] = i;
}
}
}
If you want to initialize it when the class is loaded, then you should use the static initializer:
public class NewClass {
static int[] arr; //Step 1
static {
arr = new int[10]; //Step 2
for(int i= 0;i<10;i++){
arr[i] = i;
}
}
}
Initializing a static member in the constructor defeats the purpose of static members, since they don't belong to any instance, and each new instance you'll create will override the value of your static array.
You should either initialize the static variable when it's declared or in a static initialization block.
static int[] arr = new int[10];
or
static {
arr = new int[10];
}
The initialization (i.e. the execution of the static declarations and static initialization blocks) will occur when the class is loaded, which happens when your application first accesses any member (constructor, static method, static variable) of your class.
Step 2 is be the initialization step, right?
No, It's called construction of array. The initialization of array means putting things into it that you are doing after step 2.
Then what happens in step 1 "when the class is loaded"?
when the class is loaded all static variables are initialized with their default values. In case of Object it's default value is null or you can say a reference that is pointing to nothing. No memory is allocated to array at this point of time.
What happens till Step 2 ?
When the object of type NewClass is created using keyword new at that time constructor is called and the array is constructed and memory is assigned for 10 int values in the heap with all zero as default value (till step 2)
What happens after Step 2?
After Step 2 you are actually initializing the array i.e putting the values in it.
static int[] arr; // declaration
arr = new int[10]; // construction
arr[i] = i; // initialization
If you want to read more about it then read book SCJP Sun Certified Programmer for Java 6
When the class is loaded by Class Loader, the job of linker starts. Linker verifies the Java code, whether it is written as per specifications given in Java Language & JVM. If it found valid Java Code then it starts allocating memory for fields, methods, interfaces, etc. Create a reference to that memory locations. Once reference is assigned to memory location, all field variables, methods, interfaces, etc are initialized to there default values, if not specified explicitly. Otherwise, it assigns whatever value is set as its initial value.

Reasons for restrictions on assignment of final variables

Why aren't final variables default initialized? Shouldn't the default constructor initialize them to default values if you are happy with the constant be the default value.
Why must you initialized them in the constructor at all? Why can you can't you just initialize them before using them like other variables?
ex.
public class Untitled {
public final int zero;
public static void main(String[] args)
{
final int a; // this works
a = 4; // this works, but using a field doesn't
new Untitled();
}
}
Untitled.java:2: variable a might not have been initialized
Why must you initialize static final variables when they are declared? Why can't you just initialize them before using them in any other method?
ex.
public class Untitled
{
public final static int zero;
public static void main(String[] args)
{
zero = 0;
}
}
Untitled.java:8: cannot assign a value to final variable zero
I'm asking these question because I'm trying to find a logical/conceptual reason why this won't work, why it isn't allowed. Not just because it isn't.
The idea behind a final variable is that it is set once and only once.
For instance final variables, that means they can only be set during initialization, whether at declaration, in a constructor, or an instance initialization block. For the variable to be set anywhere else, that would have to take place in a non-constructor method, which could be called multiple times - that's why this is off limits.
Similarly for static final variables, they can only be set at declaration or in a static initialization block. Anywhere else would, again, have to be in a method which could be called more that once:
public static void main(String[] args)
{
zero = 0;
main(null);
}
As for your first question, I'm assuming it's an error not to explicitly set a final variable in order to avoid mistakes by the programmer.
The Java Language Specification section 8.3.1.2 spells out the rules for final member variables:
A field can be declared final (§4.12.4). Both class and instance variables (static and non-static fields) may be declared final.
It is a compile-time error if a blank final (§4.12.4) class variable is not definitely assigned (§16.8) by a static initializer (§8.7) of the class in which it is declared.
A blank final instance variable must be definitely assigned (§16.9) at the end of every constructor (§8.8) of the class in which it is declared; otherwise a compile-time error occurs.
The JLS doesn't give reasons why the rules are they way they are. However, it might have come from experience in writing Java code, and the above rules are a way to avoid some common coding errors.
The concept of being final means that the variable value cannot change. If you could do as in your second example, then this variable would have been like any other one (i.e. not final)
I don't ave a good rational regarding your first question.
Because, when looking at your code, the Java compiler has no idea whether a given statement will be executed before an other statement. The only exceptions to this rule are code in constructors and implicit constructors, and that's why they're the only place that final fields can be assigned to.

Initialization of static final fields in Java

public class Main {
static final int alex=getc();
static final int alex1=Integer.parseInt("10");
static final int alex2=getc();
public static int getc(){
return alex1;
}
public static void main(String[] args) {
final Main m = new Main();
System.out.println(alex+" "+alex1 +" "+alex2);
}
}
Can someone tell me why this prints: 0 10 10? I understand that it's a static final variable and its value shouldn't change but it`s a little difficult to understand how the compiler initializes the fields.
It's an ordering problem. Static fields are initialized in the order that they are encountered, so when you call getc() to inititalize the alex variable, alex1 hasn't been set yet. You need to put initialization of alex1 first, then you'll get the expected result.
This situation is covered by JLS 8.3.2.3 "Restrictions on the use of Fields during Initialization".
The JLS rules allows the usage in your Question, and state that the first call to getc() will return default (uninitialized) value of alex.
However, the rules disallow some uses of uninitialized variables; e.g.
int i = j + 1;
int j = i + 1;
is disallowed.
Re some of the other answers. This is not a case where the Java compiler "can't figure it out". The compiler is strictly implementing what the Java Language Specification specifies. (Or to put it another way, a compiler could be written to detect the circularity in your example and call it a compilation error. However, if it did this, it would be rejecting valid Java programs, and therefore wouldn't be a conformant Java compiler.)
In a comment you state this:
... final fields always must be initialized at compile or at runtime before the object creation.
This is not correct.
There are actually two kinds of final fields:
A so-called "constant variable" is indeed evaluated at compile time. (A constant variable is a variable "of primitive type or type String, that is final and initialized with a compile-time constant expression" - see JLS 4.12.4.). Such a field will always have been initialized by the time you access it ... modulo certain complications that are not relevant here.
Other final fields are initialized in the order specified by the JLS, and it is possible to see the field's value before it has been initialized. The restriction on final variables is that they must be initialized once and only once during class initialization (for a static) or during object initialization.
Finally, this stuff is very much "corner case" behavior. A typical well-written class won't need to
access a final field before it has been initialized.
Static final fields whose values are not compile-time constant expressions are initialized in order of declaration. Thus when alex in being initialized, alex1 is not initialized yet, so that getc() returns default values of alex1 (0).
Note that result will be different (10 10 10) in the following case:
static final int alex1 = 10;
In this case alex1 is initialized by a compile-time constant expression, therefore it's initialized from the very beginning.
There is nothing special about static fields, it just that the compiler cannot workout that you are using a method which can access a field before its initialised.
e.g.
public class Main {
private final int a;
public Main() {
System.out.println("Before a=10, a="+getA());
this.a = 10;
System.out.println("After a=10, a="+getA());
}
public int getA() {
return a;
}
public static void main(String... args) {
new Main();
}
}
prints
Before a=10, a=0
After a=10, a=10
Class variables are not necessary to initialize, they are automatically set to their default values. If primitives (like int, short...) it's 0 (zero) for Objects it's null.
Therefore alex1 is set to 0.
Method variables must be initialized, otherwise you will get an compiliation error.
For a better explanation read http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html

Categories