Is the following statement about array right? Only uninitialized class variables get default value automatically assigned to them. Local variables never get default values automically assigned. The exception is arrays, they always get default values assigned no matter where they are defined. I declared an array in the main function, but the compiler gives an error saying that the array is not initialized. thanks.
Hi jordan, If you declare an array reference variable in a method it is not given a default value but when you initialize an array in a method, the component elements are set to the default value for their type.
Hope that helps. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform
It is a valid statement. Where ever the array is defined, wheather class, method or static main method, they GET DEFAULT VALUES ASSIGNED. Like Jane, mentioned, they get the default values based on the arrays data type. However in my opinion, since the array reference are nothing but a unique bit pattern, initialization of the reference shouldnt be a concern... Thankx
>Where ever the array is defined, wheather class, method or static main method, they GET DEFAULT VALUES ASSIGNED No! Nothing gets intialized to default values if declared in a method. Arrays are objects and default value of an array reference is null.
Just my little addition to try to round it all up: (Get it? 'round up'?) As Paul said, an array declared as a class or instance variable will be initialized to null. That is the value of the array variable not the values of the elements of the array. In a method the array variable, just like all other variables, is not initialized. However, with an array, when you actually use new to construct it, then its individual elements are assigned their default values. In a sense arrays behave the same as other variables regarding initialization and default values depending on where they are created. It is the members of the array that behave slightly differently (or contrary to what you would expect) when the array is actually allocated in a method. hope that summed everything up for you Dave [This message has been edited by Dave Vick (edited July 24, 2001).]
Get out of my mind! Look! A tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop