Program #1 declares the variable, but doesn't associate an instance with it. The compiler edits are designed to catch this. This variable has local method scope.
Program #2 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. [ May 18, 2005: Message edited by: Byron Estes ]
thanks for your help...sorry Still i am Unclear...please give a easy explanation...
posted 13 years ago
When you declare a non-primative variable (i.e. object), it's capable of pointing to an instance of the object type declared.
Until you instantiate an instance of the object using the "new" keyword and assign it to the variable, the variable is null.
In the case of an array, you declare it and you instantiate it. So far so good. The variable points to an instance of the array type you declared.
The DIFFERENCE here is that an ARRAY IS A COLLECTION OF VARIABLES, you simply access them via an index (i.e. objects). When the array is instantiated it doesn't fill all the variables/buckets it contains. It simply provides you with variables capable of receiving/holding pointers to the instances.
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. [ May 18, 2005: Message edited by: Byron Estes ]
The meaningful difference in the two situations is that the values in an array (whether they are primitives, as in "int  x", or objects, as in "String  x") are assigned a default value, just the same as instance variables. Method-local (automatic) variables are not.
In the case of the array, the array variable arr is a method-local variable - and therefore must be initialized before it is accessed, or a compiler error will be found.
But once the array is instantiated, the individual elements within the array need not be initialized - they get their default value, which is null for object types, 0 for numeric values, false for booleans, etc.
I broke out one of the lines in your example to show the distinction:
Another thing that might help is to think of reference variables as having three states:
1. declared, but not initialized - gives the "might not have been initialized" compiler error when you try to access it
2. declared and initialized, but null - gives "NullPointerException" runtime exception when you try to access it (apply the dot operator on it).
3. declared and referencing an object (not null) - everyone's happy
Instance variables and array elements get to skip state # 1 - they are automatically initialized to null. So you can't ever get a "might not have been initialized" compiler error.
Local variables (like your "A s" in your first program) can be in all three states.
Note, state 2 doesn't apply when you're talking about a primitive, which can't be null. It's either uninitialized (if it's local and you have not assigned a value), or set.
Hope this helps.
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Download Free Java APIs to Work with Office Files and PDF