JAVA中遇到的一些基础问题报错整合

  • Cannot make a static referance to the non-static field a.
public class Demo {
     int a;
     public static void main(String []args) {
    	 a=1;
     }
} 

原因:主方法只能是static方法(类方法),而类方法不能操作实例变量

解决方法:声明int a改成 static int a,这样a就是一个类变量了。

  • Sytax error on ";",,expected
public class Demo {
     int a;
     a=5;
} 

原因:java中只能在方法体中进行赋值。类体之中、方法体之外不能进行赋值。

解决方法:将赋值语句放在大括号中,第二行改成{a=5};

或者写成这样:

public class Demo {
     int a=5;
} 

这样叫做对a进行初始化,也是可以的。

  • The type of the expression must be an array type but it resolved to xx;
JButton[] jb=new JButton()[4];

原因:声明对象数组时右边的小括号需要省略

解决方法:改成JButton[] jb=new JButton[4];

扫描二维码关注公众号,回复: 8821174 查看本文章
发布了29 篇原创文章 · 获赞 6 · 访问量 3412

猜你喜欢

转载自blog.csdn.net/qq_42138454/article/details/102586353
今日推荐