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

报错原文: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) {
              …………
       }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值