Cannot make a static reference to the non-static method的解决方法
来源:互联网 发布:java上传excel到服务器 编辑:程序博客网 时间:2024/06/06 07:50
- 报错原因:在一个类中写了一个public String getContent()方法和一个main()方法,getContent()方法中包含了getClass()方法,在main()方法中直接调用了getContent()就出现如题的错误。这样一样
- 解决方法:先实例化类,然后再调用getContent()就没有问题了
- 这样一来都不要加static了
- GetProperties gp = new GetProperties();
- String s = gp.getCotent();
- 说明:在静态方法中,不能直接访问非静态成员(包括方法和变量)。因为,非静态的变量是依赖于对象存在的,对象必须实例化之后,它的变量才会在内存中存在。例如一个类 Student 表示学生,它有一个变量String address。如果这个类没有被实例化,则它的 address 变量也就不存在。而非静态方法需要访问非静态变量,所以对非静态方法的访问也是针对某一个具体的对象的方法进行的。对它的访问一般通过 objectName.methodName(args……) 的方式进行。而静态成员不依赖于对象存在,即使是类所属的对象不存在,也可以被访问,它对整个进程而言是全局的。因此,在静态方法内部是不可以直接访问非静态成员的。
- Code如下:
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- public class GetProperties {//不用static
- public String getCotent(){//不用static
- String content=”";
- try {
- Properties properties = new Properties();
- InputStream is = getClass().getResourceAsStream(“test.properties”);//ok
- //InputStream is = getClass().getClassLoader().getResourceAsStream(“test.properties”); //ERROR:Exception in thread “main” java.lang.NullPointerException
- properties.load(is);
- is.close();
- content = properties.getProperty(“str1″);
- } catch (FileNotFoundException ex) {
- ex.printStackTrace();
- }catch (IOException ex) {
- ex.printStackTrace();
- }
- return content;
- }
- public static void main(String[] args){
- GetProperties gp = new GetProperties();//实例化
- String s = gp.getCotent();
- System.out.println(s);
- }
- }
- test.properties中内容为str1=123
0 0
- Cannot make a static reference to the non-static method的解决方法
- Cannot make a static reference to the non-static method的解决方法
- Cannot make a static reference to the non-static method的解决方法
- Cannot make a static reference to the non-static method的解决方法
- Cannot make a static reference to the non-static method的解决方法
- Cannot make a static reference to the non-static method的解决方法
- Cannot make a static reference to the non-static method
- Cannot make a static reference to the non-static method
- Cannot make a static reference to the non-static method
- Cannot make a static reference to the non-static method的解决方案
- Cannot make a static reference to the non-static method setInAnimation(Animation) from the type View
- Cannot make a static reference to the non-static method getLocalActivityManager() from the type Acti
- (转)Cannot make a static reference to the non-static method
- Cannot make a static reference to the non-static method xxx()
- ava错误Cannot make a static reference to the non-static method
- 自学JAVA之Cannot make a static reference to the non-static method分析
- Java Cannot make a static reference to the non-static method
- “Cannot make a static reference to the non-static method”处理方法
- Android APK反编译详解(附图)
- javascript复习--全局变量与局部变量
- Google I/O 2008-Inside the Android Application Framework
- listview优化
- 在VMware Workstation上安装Kali Linux
- Cannot make a static reference to the non-static method的解决方法
- 1.4.2 收集ash报表 3
- andorid中dispatchkeyevent事件传递及焦点获取
- 计算机网络笔试题目
- a500从拨号到通话界面显示的过程
- 如何为一个对象赋默认值
- 指针函数和函数指针
- Java实现简单的Socket服务器与客户端字符串通讯(适合初学者阅读)
- 天易37-----linux下安装配置tomcat7