• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array

 
sweety sinha
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
sweety sinha
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i got it
thanks guys for quick response.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between declaring a variable/array as static and declaring inside a static initializer?
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.... ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic