This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array as a local variable Initialization
 
Similar Threads
Clarification please for Chapter1 / Question 7
Small Doubt
var arg in method
pls help exams are on my head
accessing a protected access instance variable defined at a superclass