Cannot make a static reference to the non-static method

后端 未结 7 2117
长发绾君心
长发绾君心 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 04:54

    getText is a member of the your Activity so it must be called when "this" exists. Your static variable is initialized when your class is loaded before your Activity is created.

    Since you want the variable to be initialized from a Resource string then it cannot be static. If you want it to be static you can initialize it with the String value.

    0 讨论(0)
  • 2020-11-21 04:55

    You can either make your variable non static

    public final String TTT =  (String) getText(R.string.TTT);
    

    or make the "getText" method static (if at all possible)

    0 讨论(0)
  • 2020-11-21 04:59

    There are some good answers already with explanations of why the mixture of the non-static Context method getText() can't be used with your static final String.

    A good question to ask is: why do you want to do this? You are attempting to load a String from your strings resource, and populate its value into a public static field. I assume that this is so that some of your other classes can access it? If so, there is no need to do this. Instead pass a Context into your other classes and call context.getText(R.string.TTT) from within them.

    public class NonActivity {
    
        public static void doStuff(Context context) {
            String TTT = context.getText(R.string.TTT);
            ...
        }
    }
    

    And to call this from your Activity:

    NonActivity.doStuff(this);
    

    This will allow you to access your String resource without needing to use a public static field.

    0 讨论(0)
  • 2020-11-21 05:05

    Since getText() is non-static you cannot call it from a static method.

    To understand why, you have to understand the difference between the two.

    Instance (non-static) methods work on objects that are of a particular type (the class). These are created with the new like this:

    SomeClass myObject = new SomeClass();
    

    To call an instance method, you call it on the instance (myObject):

    myObject.getText(...)
    

    However a static method/field can be called only on the type directly, say like this: The previous statement is not correct. One can also refer to static fields with an object reference like myObject.staticMethod() but this is discouraged because it does not make it clear that they are class variables.

    ... = SomeClass.final
    

    And the two cannot work together as they operate on different data spaces (instance data and class data)

    Let me try and explain. Consider this class (psuedocode):

    class Test {
         string somedata = "99";
         string getText() { return somedata; } 
         static string TTT = "0";
    }
    

    Now I have the following use case:

    Test item1 = new Test();
     item1.somedata = "200";
    
     Test item2 = new Test();
    
     Test.TTT = "1";
    

    What are the values?

    Well

    in item1 TTT = 1 and somedata = 200
    in item2 TTT = 1 and somedata = 99
    

    In other words, TTT is a datum that is shared by all the instances of the type. So it make no sense to say

    class Test {
             string somedata = "99";
             string getText() { return somedata; } 
      static string TTT = getText(); // error there is is no somedata at this point 
    }
    

    So the question is why is TTT static or why is getText() not static?

    Remove the static and it should get past this error - but without understanding what your type does it's only a sticking plaster till the next error. What are the requirements of getText() that require it to be non-static?

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 05:09

    This question is not new and existing answers give some good theoretical background. I just want to add a more pragmatic answer.

    getText is a method of the Context abstract class and in order to call it, one needs an instance of it's subclass (Activity, Service, Application or other). The problem is, that the public static final variables are initialized before any instance of Context is created.

    There are several ways to solve this:

    1. Make the variable a member variable (field) of the Activity or other subclass of Context by removing the static modifier and placing it within the class body;
    2. Keep it static and delay the initialization to a later point (e.g. in the onCreate method);
    3. Make it a local variable in the place of actual usage.
    0 讨论(0)
提交回复
热议问题