Two Laptop Bag
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array as a local variable Initialization" Watch "Array as a local variable Initialization" New topic

Array as a local variable Initialization

Sanjay Mittal

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.
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.
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
Hope that helps.

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
I agree. Here's the link:
subject: Array as a local variable Initialization
jQuery in Action, 3rd edition