• 报错原因:在一个类中写了一个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的解决方法的更多相关文章

  1. Cannot make a static reference to the non-static

    public class SeckillServiceImpl implements SeckillService{ private SeckillDao seckillDao; private Su ...

  2. “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 ...

  3. java 中 Cannot make a static reference to the non-static 解决方法

    今天敲代码的时候遇到了这个问题,大体这个问题可以简化成这样: public class Test1 { public String get() { return "123"; } ...

  4. static 类成员变量 和 static const类成员变量

    1.使用static类的优点: (1)避免与其他类的成员或者全局变量冲突 (2)可以封装 (3)阅读性好 2.static 数据成员独立于该类的任意对象而存在 static数据成员的类型可以是该成员所 ...

  5. static与C#中的static

    Static 1.静态方法与非静态方法 a.静态方法的调用类.静态方法名([参数列表]) 非静态方法的调用类 对象 = new 类的构造函数([参数列表])对象.非静态方法名([参数列表]) 静态方法 ...

  6. Java中的static关键字解析(转自海子)__为什么main方法必须是static的,因为程序在执行main方法的时候没有创建任何对象,因此只有通过类名来访问。

    Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...

  7. 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: ...

  8. java中static特殊性和final(static成员直接被访问,this不能用在static方法中,static不可访问非static)

    java的static关键字 java中,static修饰的成员变量和成员方法叫静态变量和静态方法,不依赖类特定的实例,被类的所有实例共享. 静态变量或类变量 和 实例变量,区别是: 静态变量在内存中 ...

  9. 问题:eclipse中线程编程编译报错,undefined reference to 'pthread_create'的解决方法(已解决)

    问题描述: 在Ubuntu系统中,使用eclipse CDT集成开发环境编写pthread程序,编译时,pthread_create不通过,报错信息是: undefined reference to ...

随机推荐

  1. Express使用MongoDB常用操作

    const MongoClient = require('mongodb').MongoClient const url = "mongodb://localhost:27017" ...

  2. 2. let和const命令--ES6

    1. let命令 let 命令不存在变量提升let 命令 只在let命令所在的代码块内有效. { let a = 10; var b = 1; } a // ReferenceError: a is ...

  3. 【BZOJ 3994】3994: [SDOI2015]约数个数和(莫比乌斯反演)

    3994: [SDOI2015]约数个数和 Description  设d(x)为x的约数个数,给定N.M,求   Input 输入文件包含多组测试数据. 第一行,一个整数T,表示测试数据的组数. 接 ...

  4. BZOJ 1202 [HNOI2005]狡猾的商人(并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1202 [题目大意] 给出一些区间和的数值,问是否存在矛盾 [题解] 用并查集维护前缀和 ...

  5. MySQL v5.7.18 版本解压安装

    下载MySQL https://dev.mysql.com/downloads/mysql/5.1.html#downloads 个人机子是64位的,所以选择下载:Windows (x86, 64-b ...

  6. #iOS问题记录# UITextview富文本链接,禁止长按事件

    UITextView的富文本组装,添加图片点击事件,启动 - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *) ...

  7. IE主页被篡改为360导航终极处理办法

    不得不吐槽360真他妈是流氓至极,在不被用户许可的情况下直接修改用户浏览器默认主页,还不容易找到修改的地方. 以IE为例:我们将其首页设置为百度https://www.baidu.com/,终极处理办 ...

  8. What happens when a SQL Query runs?

    Posted by Padma Chitturi in Uncategorized. Leave a Comment Hi Folks, It has been such a long time th ...

  9. GNU C内联汇编(AT&T语法)

    转:http://www.linuxso.com/linuxbiancheng/40050.html 内联汇编提供了可以在C或C++代码中创建汇编语言代码,不必连接额外的库或程序.这种方法对最终程序在 ...

  10. BootstrapValidator

    一.引入必要文件 <link rel="stylesheet" href="/path/to/bootstrap/css/bootstrap.css"/& ...