public class MyClass{
final int MAXX=640, MAXY=480; // low-res (VGA)
int x, y; // current location
/** Construct a MyClass with x and y values */
MyClass(int x, int y) {
this.x = x;
this.y = y;
}
/** Construct a MyClass with default values */
MyClass() {
this(MAXX/2, MAXY/2); // Use the constructor above
}public String toString() 
{
return "[" + x + "," + y + "]";
}/** Test main program */
public static void main(String[] av) {
System.out.println(new MyClass(300,100));
System.out.println(new MyClass());
}
}javac时报错:
error:
MyClass.java:15: Can't reference MAXX before the superclass constructor has be called.
this(MAXX/2, MAXY/2); // Use the constructor above
^
MyClass.java:15: Can't reference MAXY before the superclass constructor has be called.
this(MAXX/2, MAXY/2); // Use the constructor above
^
2 errors请问问题出在哪,好像是MAXX,MAXXY定义的问题,先谢过了

解决方案 »

  1.   


    MyClass() {
    this(MAXX/2, MAXY/2); // Use the constructor above
    }上面那句有问题,不明白你要做什么,改成:MyClass() {
    this.x = MAXX/2;
    this.y = MAXY/2;// Use the constructor above
    }构造函数能对自身的一些成员变量做一些初始,感觉你的写法好像C++
      

  2.   

    将final int MAXX=640, MAXY=480;
    改成 static final int MAXX=640, MAXY=480;就可以了 !
      

  3.   

    这是因为this(MAXX/2, MAXY/2); 的执行在final int MAXX=640, MAXY=480初始化之前(我是这样认为的); 所以要将它改为static .
      

  4.   

    就算 那个不是final型的,也会出错的 !
      

  5.   

    static final int MAXX=640, MAXY=480
      

  6.   

    this(MAXX/2, MAXY/2);
    这是什么意思能给菜鸟解释下吗???这里的this指带的是什么呀?
      

  7.   

    这里的this是this的第二个作用,即用来调用类的其他构造器
      

  8.   

    static final int MAXX=640, MAXY=480 可以解决问题,
    但建议使用:yaowenjie1981(小渣)的
    MyClass() {
    this.x = MAXX/2;
    this.y = MAXY/2;// Use the constructor above
    }
      

  9.   

    chinajavaworld.com  drmcer的回复:    错误信息已经说的很清楚了, 不能在超类的构造函数被调用之前引用MAXX和MAXY变量.
    因为MAXX与MAXY变量是非静态成员属性, 因此只能在对象被创建了之后才可以调用, 但此时this(MAXX / 2, MAXY / 2);这一句, 由于你得先取得MAXX与MAXY的值后才能传递参数到this中, 所以这就属于在还没有调用超类的构造函数之前就要取得当前类的成员, 这当然是不允许的(因为当前对象还没有创建完成).
        你可以将MAXX与MAXY定义成static的(至于是不是final无关紧要, 当然这取决于你是否想让它们可被修改), 这样就不会有问题了. 因为static成员不与对象相关联
       
      

  10.   

    satchi_2002() 是正解啊,不能在创建对象之前调用非静态数据!