Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array question

 
Tosin Adedoyin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is question is real easy and prob silly but i was trying out arrays declaration and construction
i did this
public class Point {
static int npoints;
int x;
int y;
Point root;
int[] intoos;
intoos = new int[23];


public static void main(String[] args) {
System.out.println("npoints=" + Point.npoints);
Point p = new Point();
System.out.println("p.x=" + p.x + ", p.y=" + p.y);
System.out.println("p.root=" + p.root);
int[] ints;
ints = new int[23];

}
the diffrent declartion in main works fine but the one in the class declaration i had to this
int[] intoos= new int[23];before the class complied in the code above i got the following errors why ??
intoos = new int[23];
^
cannot resolve symbol
symbol : class intoos
location: class Point
intoos = new int[23];
^
2 errors
just wondering why it will not let me do this
int[] intoos;
intoos = new int[23];
within the class
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int[] intoos;
intoos = new int[23];
you cannot put the array creation expression outside a block.
Either you put it that way:
int[] intoos = new int[23];
outside of any block (where it is right now)
or you have to put the "intoos = new int[23];" within a method or a constructor.
You can see that the same declaration in the main does not cause any error.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tosin,
If you combine the "declaration" and the "definition" this will compile fine like this:

Now, if you want to separate the declaration from the definition then you need a "constructor" maybe like this:

Hope this helps
Michael Morris
SCJP
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic