aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array as a local variable Initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array as a local variable Initialization" Watch "Array as a local variable Initialization" New topic
Author

Array as a local variable Initialization

Sanjay Mittal
Greenhorn

Joined: Oct 29, 2000
Posts: 1
Hi all
I am reading all these posting and it is really helpful for my preparation. I am going to give test next Monday. Please help me to clear my confustion.
RHE books says Arrays are initialize when we declare them and it doesn't make any difference if they are local to a method.
If you compile code given below, it will give you compilation error.
public class test
{ public static void main(String args[])
int[] a, b[] ;
int c[][];
c = b; // Both are two dimensional
}
}
It will give compilation error that b is not intialized. I don't know why.
TIA
Sanjay
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Sanjay,
An array is initialized when it's created (dimensioned) not when it's declared.
<pre>
public static void main(String[] args) {
int[] a, b[]; // declare arrays
int c[][]; // declare array
b = new int[3][3]; // create array
c = b; // compiles without error
}
</pre>
Hope that helps.
------------------
Jane


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
 
subject: Array as a local variable Initialization