• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Array declaration error

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9099
12
  • Mark post as helpful
  • send pies
  • 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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!