public class Array
{
static String[] a = new String[] {"red", "green", "blue"};
static Point[] p = new Point[] {new Point(1, 2), "3,4"};
public static void main(String[] args)
{
System.out.println("hello");
}
class Point
{
int x;
int y;
Point(int x, int y)
{
this.x = x;
this.y = y;
}
Point(String s)
{
String[] a = s.split(",");
x = a[0].parseInt();
y = a[1].parseInt();
}
}
}
In the above program, the static Point array initialization fails, reporting error:
Array.java:4: non-static variable this cannot be referenced from a static context
static Point[] p = new Point[] {new Point(1, 2), "3,4"};
But, the static String array succeeds. What's the difference between them?
I really need a static object array, because it's easy to refer to without instantiate the outer class.
Thanks