Forums Register Login

Can anyone explain please

+Pie Number of slices to send: Send
Hi:
Just on the question below. Why is line 5 not compiling. It must be something very simple I am missing here...any help would be appreciated
1. class ArrayTest {
2.
3. static int [ ] intArray = new int [5] ;
4. static int [ ] intArray1 = new int [1] ;
5. intArray1 [0] = 5 ;
6. static char [ ] charArray = new char [5] ;
7.
8. public static void main (String [ ] args) {
9.System.out.println (charArray [1] ) ;
10.intArray1 = intArray ;
11.intArray = charArray ;
12. }
13. }

a. The compiler objects to line 5.
b. The compiler objects to line 10.
c. The compiler objects to line 11.
d. Omitting lines 5,10 and 11 would make the program print 0.
e. There is nothing wrong with the code and the program prints 0
+Pie Number of slices to send: Send
Line 5 is an assignment statement so it must appear in a code block such as a method or constructor body or an initializer block.
+Pie Number of slices to send: Send
Hi Soum,
You can declare and initialize an array at the same time. But you can not initialize it seperately out side the method.
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 762 times.
Similar Threads
default value for Array elements
static array question
Array Assignment
Arrays
Array
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 18:26:41.