“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) {
…………
}
}
报错原因:在一个类中写了一个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) {
…………
}
}
相关文章推荐
- css3
- #!/usr/bin/env node\n
- Gson 用户指南(中文)
- Script入门篇
- html屏蔽鼠标右键
- Json的发送与解析
- jquery的常见使用
- 30+有用的CSS代码片段
- 纯 CSS 创建各种不同的图形形状
- 自定义登陆界面样式
- 修改html页面文字选中样式
- AngularJS 学习笔记 (三)
- 让你的博客代码示例在 jsfiddle 里运行
- Ember.js--入门
- js兼容火狐显示上传图片预览效果的方法(读取字节)
- 每天学习十分钟11之JavaScript
- javascript单例模式与桥接模式
- html5实战2
- 搜索栏模糊查询特效--jquery插件
- bootstrap-treeview 自定义实现双击事件