Cannot make a static reference to the non-static method的解决方法
- 报错原因:在一个类中写了一个public String getContent()方法和一个main()方法,getContent()方法中包含了getClass()方法,在main()方法中直接调用了getContent()就出现如题的错误。这样一样
- 解决方法:先实例化类,然后再调用getContent()就没有问题了
GetProperties gp = new GetProperties();
String s = gp.getCotent();- 这样一来都不要加static了
- 说明:在静态方法中,不能直接访问非静态成员(包括方法和变量)。因为,非静态的变量是依赖于对象存在的,对象必须实例化之后,它的变量才会在内存中存在。例如一个类 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
Cannot make a static reference to the non-static method的解决方法的更多相关文章
- Cannot make a static reference to the non-static
public class SeckillServiceImpl implements SeckillService{ private SeckillDao seckillDao; private Su ...
- “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 ShapeTes ...
- java 中 Cannot make a static reference to the non-static 解决方法
今天敲代码的时候遇到了这个问题,大体这个问题可以简化成这样: public class Test1 { public String get() { return "123"; } ...
- static 类成员变量 和 static const类成员变量
1.使用static类的优点: (1)避免与其他类的成员或者全局变量冲突 (2)可以封装 (3)阅读性好 2.static 数据成员独立于该类的任意对象而存在 static数据成员的类型可以是该成员所 ...
- static与C#中的static
Static 1.静态方法与非静态方法 a.静态方法的调用类.静态方法名([参数列表]) 非静态方法的调用类 对象 = new 类的构造函数([参数列表])对象.非静态方法名([参数列表]) 静态方法 ...
- Java中的static关键字解析(转自海子)__为什么main方法必须是static的,因为程序在执行main方法的时候没有创建任何对象,因此只有通过类名来访问。
Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...
- windows python flask上传文件出现IOError: [Errno 13] Permission denied: 'E:\\git\\test\\static\\uploads'的解决方法
在浏览器中输入时,出现IOError: [Errno 13] Permission denied: 'E:\\git\\test\\static\\uploads' http://127.0.0.1: ...
- java中static特殊性和final(static成员直接被访问,this不能用在static方法中,static不可访问非static)
java的static关键字 java中,static修饰的成员变量和成员方法叫静态变量和静态方法,不依赖类特定的实例,被类的所有实例共享. 静态变量或类变量 和 实例变量,区别是: 静态变量在内存中 ...
- 问题:eclipse中线程编程编译报错,undefined reference to 'pthread_create'的解决方法(已解决)
问题描述: 在Ubuntu系统中,使用eclipse CDT集成开发环境编写pthread程序,编译时,pthread_create不通过,报错信息是: undefined reference to ...
随机推荐
- Linux前后台进程切换
(1).Linux前台进程与后台进程的区别 前台进程:是在终端中运行的命令,那么该终端就为进程的控制终端,一旦这个终端关闭,这个进程也随之消失. 后台进程:也叫守护进程(Daemon),是运行在后台的 ...
- 设计模式-适配器模式(Adapter Pattern)
本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 适配器模式简介 适配器模式的作用就如同现实生活中转接头的作用一样,现实生活中我们会用USB转接头 ...
- Codeforces Round #448(Div.2) Editorial ABC
被B的0的情况从头卡到尾.导致没看C,心情炸裂又掉分了. A. Pizza Separation time limit per test 1 second memory limit per test ...
- Activity(活动)生命周期(3)--活动的生存期
Activity类中定义了7中回调方法,覆盖了活动生命周期的每一个环节. 回调方法: 1.onCreate() 这个方法会在活动第一次被创建的时候调用.我们应该在这个方法中完成活动的初始化操作,比如: ...
- Highcharts做统计图。
<script> $(function () { var areasplineData = [[1447916401000,3],[1447918201000,4]]; var typeR ...
- CentOS 6.9下配置安装KVM
注意:KVM一切安装和运行都是在root用户下完成的,并且只有root才能支持某些软件. 一.准备工作: 1.查看系统版本.内核版本 ##查看系统版本 # cat /etc/redhat-releas ...
- 一次完整的HTTP请求的大致过程(转)
说明:这些理论基本都来自网上,所以不一定准确,但一定是比较好理解的,如果要刨根问底,最好的方式就是看书,且要看权威的书. 一次完整的HTTP请求所经历的7个步骤 HTTP通信机制是在一次完整的HTTP ...
- PLM_百度百科
PLM_百度百科 ZDLINK
- pycharm的插件pylint报错:java.lang.Throwable: Write-unsafe context! Model changes are allowed from write-safe contexts only. Please ensure you're using invokeLater/invokeAndWait with a correct modality stat
java.lang.Throwable: Write-unsafe context! Model changes are allowed from write-safe contexts only. ...
- iOS:多线程的详细介绍
多线程: 一.概念 1.什么是进程? 程序的一次性执行就是进程.进程占独立的内存空间. 2.什么是线程? 进程中的代码的执行路径. 3.进程与线程之间的关系? 每个进 ...