Cannot make a static reference to the non-static method

后端 未结 7 2120
长发绾君心
长发绾君心 2020-11-21 04:32

Building a multi-language application in Java. Getting an error when inserting String value from R.string resource XML file:

public static final         


        
7条回答
  •  温柔的废话
    2020-11-21 05:08

    You can not make reference to static variable from non-static method. To understand this , you need to understand the difference between static and non-static.

    Static variables are class variables , they belong to class with their only one instance , created at the first only. Non-static variables are initialized every time you create an object of the class.

    Now coming to your question, when you use new() operator we will create copy of every non-static filed for every object, but it is not the case for static fields. That's why it gives compile time error if you are referencing a static variable from non-static method.

提交回复
热议问题