File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array question" Watch "array question" New topic

array question

Tosin Adedoyin
Ranch Hand

Joined: Nov 09, 2001
Posts: 43
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

Joined: Aug 26, 2001
Posts: 7610
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.

[Blog] [Blogroll] [My Reviews] My Linked In
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
I agree. Here's the link:
subject: array question
jQuery in Action, 3rd edition