您的位置:首页 > Web前端

“Cannot make a static reference to the non-static method”处理方法

2016-01-08 19:50 501 查看
报错原文:Cannot make a static reference to the non-static method maxArea(Shape[]) from the type ShapeTestb
 
报错原因:在一个类中写了一个public void maxArea ()方法和一个main()方法,在main()方法中直接调用maxArea()方法就出现如题的错误。
 
解决方法,有两种:
方法一、maArea()定义时加上修饰符static,即变为public static void maxArea(),即可在main()方法中正确调用。
 public class ShapeTestb {
public static void main(String[] args) {
Shape[]shapes = new Shape[2];
maxArea(shapes);
}
//在main()中编译错误
//public void maxArea(Shape[] shapes) {
//在main()中编译正确
public static void maxArea(Shape[]shapes) {
…………
}
}
 
方法二、先实例化类,然后通过对象调用maxArea()方法也行。
 public class ShapeTestb {
public static void main(String[] args) {
Shape[] shapes = new Shape[2];
ShapeTestb st = new ShapeTestb();
st.maxArea(shapes);
}
public void maxArea(Shape[] shapes) {
…………
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: