Wednesday, October 26, 2005

Java Class Loading

Learn it here and here

Array Initialization

class MyHello {

public static void main(String[] args) {

A[] arr=new A[5];//Creating array of references of type class A (null values by default)

// arr[1]=new A();//assigning new object to arr[1] reference variable(overrides null value)

System.out.println(arr[1].a);

}

}

class A{

int a;

}

// O/p ==> java.lang.NullPointerException

/*

The program declares the variable and instantiates the object array to a size of 5. While the array variable refers to a valid instance of the array. The contents of the array which should refer to instances of the objects of the type declared, were never created/initialized. An array is simply a collection of references the references may or may not refer to a valid instance on the object of the collection type. As a result, if it's possible that you will get unitialized instances in your array/collection it's usually a good idea to check that the item is not null before you make any method calls on it.

Whenever you are using an array of objects you usually go through a 3 step process where you declare it, instantiate it and you fill it with instances of the type it was declared to hold.

*/

0 Comments:

Post a Comment

<< Home