• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Array declaration error

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I've a code like :
/* this is a simple program */
class test {
int z[];
z = new int[5];
}
class Example {
public static void main(String args[]) {
//dummy class calling the main method
}
}
When i compile the above program I get errors at the memory allocation statement :
z = new int[5];
I don't find anything wrong with the array declaration. Can anyone tell me why is that ?
Thanx,
Vijay
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That statement is not legal in that position. You are making a statement not inside a method.
Either do the initialization when you declare the array as in:
int[] z = new int[5]; //note the brackets on the int, please don't use them after the z, it's bad style.
OR you have to put your initialization code in a method, maybe a constructor


Rob
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There is nothing wrong with the array declaration, but if you don't initialize when you declare ( for example:
int[] z = new int[5] ; ),
you must put the initialization inside some curley braces (for instance, inside a method).

[ January 28, 2002: Message edited by: Marilyn deQueiroz ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic