“A Non-static method cannot be referenced from a static context” error


Getting an error when inserting String value from R.string resource XML file:

public static final String TT = (String) getText(R.string.TT);

This is the error message:

Error: Cannot make a static reference to the non-static method getText(int) from the type Context

 how can I solve it?



It is not possible to make reference to a static variable from a non-static method. To understand this, you need to understand the difference between static and non-static.

Static variables are referred to as the  class variables, they belong to the 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 a copy of every non-static field for every object, but it is not the case for static fields. That's why it gives a compile time error if you are referencing a static variable from a non-static method.


If you want to unleash your potential in this competitive field, please visit the Java course page for more information, where you can find the Java tutorials and Java frequently asked interview questions and answers as well.


This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.