This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array" Watch "array" New topic
Author

array

sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
10. public class Foo {
11. static int[] a;
12. static { a[0]=2; }
13. public static void main( String[] args) {}
14. }

this on execution is giving ExceptionInInitializerError
but if line11 is initialised static int[] a = new int[2];
then it compile and runs fine
OR
when from line12 static is removed it compile and runs fine as given below
static int[] a;
{ a[0]=2; }
so is the problem due to static modifier or due to initialisation is not done or both
Bharath Raghavendra
Greenhorn

Joined: Jul 07, 2008
Posts: 8
Static initializer runs once the class is loaded on to the JVM.
If you remove the 'static' the initializer runs everytime you instantiate an object of Foo . Try instantiating the Foo in the main and check the result
Bharath Raghavendra
Greenhorn

Joined: Jul 07, 2008
Posts: 8
That was because as 'a' is static it can be initialized only once and use it through any instances of Foo .In this case 'a' can be initialized without instantiating the Foo class . Check the following code

public class Foo {

static int[] a;

{a[0]=2;}

public static void main(String[] args)
{
Foo.a = new int[2];
Foo f = new Foo();
System.out.println("In main");
}

}

It will run fine without exception.

If we dont put "Foo.a = new int[2];" it will give NullPointerException.

So compiler expects the 'a' array to be initialized outside the instance, hence it gives the exception at runtime.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

whenever any exception occurs in a static initializer block or expression, then ExceptionInInitializerError is thrown.

Since you have not initialized the array a, so it is assigned null. So calling a[0] = something, will result in a NullPointerException, and thus the JVM will throw ExceptionInInitializerError.

You said that removing static from the intializer removes the error, try to make an instance of the class after removing static from the intializer. You will get a NullPointerException...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
now i got it
thanks guys for quick response.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
What is the difference between declaring a variable/array as static and declaring inside a static initializer?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

@abhi if I got you right, you are talking about this



Whatever the case is, both the arrays are initialized and have a size of 3 and 2 respectively.

Also remember that there is a difference between array initialization and array elements initialization.

int[] arr;

the array is now declared

arr = new int[2];

the array is now initialized. whenever you initialize an array, the elements in the array are set to their default values(boolean -> false, char -> \u000, integers -> 0, float - 0.0).
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks a lot,Ankit. I was under the impression that once an array is declared,its elements are given default values.But, I have one more doubt, is there a difference between initializing variables/arrays inside a static initializer.... ?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: array