Solution for :Cannot make a static reference to the non-static method
2016-01-15 13:34
585 查看
最近在学习java,作为java的菜鸟和以前C语言思维带来的影响,在弄java时经常犯一些比较低级的错误,弄清楚这些低级的错误对于理解java有很好的帮助。所以记录一下。
Cannot make a static reference to the non-static method 这个错误估计是最常见的,当时还觉得奇怪为什么是这样的情况呢。
比如我写了下面 一段java的测试小程序来检验一个字符串是否是数字:
You can't make a static reference to the non-static method, so you can change the un-static method to a static method or you need to create an object using new operator and call the method by object.method().
stackoverflow上explaination:
http://stackoverflow.com/questions/23860661/cannot-make-a-static-reference-to-the-non-static-method
Cannot make a static reference to the non-static method 这个错误估计是最常见的,当时还觉得奇怪为什么是这样的情况呢。
比如我写了下面 一段java的测试小程序来检验一个字符串是否是数字:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class TestStrIsNum { public boolean isNumeric(String str){ Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+"); Matcher isNum = pattern.matcher(str); if( !isNum.matches() ){ return false; } return true; } public static void main(String[] argv) { String a =new String("-1000"); //if (isNumeric(a)) error:Cannot make a static reference to the non-static method TestStrIsNum test = new TestStrIsNum(); if (test.isNumeric(a)) System.out.println("a = -1000 is a number"); } }Analysis and soultion:
You can't make a static reference to the non-static method, so you can change the un-static method to a static method or you need to create an object using new operator and call the method by object.method().
stackoverflow上explaination:
http://stackoverflow.com/questions/23860661/cannot-make-a-static-reference-to-the-non-static-method
相关文章推荐
- ExtJS快速入门指南
- CSS 注意事项
- html5网页动画总结--jQuery旋转插件jqueryrotate
- javascript自定义浏览器右键菜单
- jQuery版本冲突解决办法
- JS 跨域访问的几种方式
- Html canvas 五子棋
- JavaScript的异步执行的运行机制和setTimeout的双重求值
- 关于JAVA EE项目在WEB-INF目录下的jsp页面如何访问WebRoot中的CSS和JS文件
- Leetcode 237 Delete Node in a Linked List 链表
- nw.js如何处理拖放操作
- css学习笔记20160115css语法格式选择器
- jsp URL中文参数传递
- 【问题:发现与解决】angularJs指令在dijit控件中的使用
- Jquery scroll 向下滚动到据顶部超过1000px时,回到顶部
- web前端开发你应该知道的好消息
- Jquery Scroll 事件
- jQuery插件imgPreviewQs实现上传图片预览
- Caffe Convolutional Layer
- ArcGIS API for JavaScript Beta初步试探(一)