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

+2 votes

Announcement! Career Guide 2019 is out now. Explore careers to become a Big Data Developer or Architect!

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?

Jun 25, 2018 in Java by parth295
• 4,640 points
51,733 views

12 answers to this question.

0 votes
You can not 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 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 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 a non-static method.
answered Jun 26, 2018 by samarth295
• 2,190 points
no.............
this is clear and helpful, thank you
And also very wrong
0 votes
This reference is not allowed.
answered Dec 7, 2018 by abc
0 votes

Static methods are connected to the class itself and not its instances. For e.g. if you have a class A, a 'normal' method b, and a static method c, and you make an instance a of your class A, the calls to A.c() and a.b() are valid. Method c() has no idea which instance is connected, so it cannot use non-static fields.

answered Dec 7, 2018 by Trisha
0 votes

This is not allowed as when a method or variable that is not declared as static is created only when the class is instantiated as an object for example by using the new operator.

answered Dec 7, 2018 by Rishan
0 votes

As you haven't created an object, the non-static method doesn't exist yet. A static method (by definition) always exists. Hence what you are trying to do is not permissible.

answered Dec 7, 2018 by Neha
0 votes

You can can call a method from an instantiable class by appending a method call to its constructor,

Object instance = new Constuctor().methodCall(); 
primitive name = new Constuctor().methodCall(); //You can use this aswell.
answered Dec 7, 2018 by Abhinav
0 votes

The compiler has no way to guess which instance method you are referring to, if you try to access an instance method from a static context. Though, you can always access it using an object reference.

answered Dec 7, 2018 by Sona
0 votes

static method equates an action to a type of object, while a non static method equates an action to an instance of that type of object. Typically it is a method that does something with relation to the instance. This is why you are getting an error saying you cannot make a static reference to a non-static meothod getText(int) from the type Context.

answered Dec 7, 2018 by Nupur
0 votes

A non-static method is dependent on the object. It is recognized by the program once the object is created. But a static method can be called before the object creation. Hence you cannot make the reference.

answered Dec 7, 2018 by Disha
0 votes
Java doesn't allow you to access the non-static block from a static block. The reason why this happens is, a static block can be invoked before the object is created for it while a non-static block cannot be called before the object creation. So when the static block invokes the non-static block and if its object is not created you wont be able to access it.
answered Dec 7, 2018 by Dharmesh'
0 votes

Instead of final, here you can wrap protected static TT into class G and call static method G.tt()
For initial / change tt you must create G type object - it is sufficient prevention against accidental change of tt.
(I simulate your expression with Example class)

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

class G {
  // public static final String TT
  protected static String tt;
  
  G (String tt) {this.tt = tt;}
  static String tt() {return tt;}
}
  
class Main {  
  public static void main(String[] args) {  
    Example r = new Example();
                        //  = (String) getText(R.string.TT);
    G g_ini = new G( String.valueOf( r.getText(r.string_TT) ));
    g_ini = null;

    System.out.println(G.tt() );
  }
}
  
class Example { 
  String string_TT = "T123456";
  
  char getText(String s){
    return(s.charAt(0));
} }

// Output: T

answered Jan 29 by zemiak
0 votes
this method must be static so that it will exist even though the class has not actually been instantiated as an object. The JVM does not create an instance of the class by creating an object from the class. ... After creating the object you can  use the variables and methods of the object.

Anything bound to the static keyword is available in the context of the class rather than in the context of an instance of the class
Relative to the above- variables within a method can not be static and static fields, and methods must be invoked using the class-name e.g. MyClass.main()
answered Jun 11 by Neha
• 330 points
Thanks for the explanation @Neha! Can you also mention how to solve this error please?

Related Questions In Java

0 votes
2 answers

How a static map can be initialized?

public class Test { ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,880 points
81 views
0 votes
2 answers

How to return multiple objects from a Java method?

In the event the method you're calling ...READ MORE

answered Aug 30 in Java by Karan
• 3,250 points
115 views
0 votes
1 answer

Non-static method within Static method in Java

As per my knowledge, you are getting this error ...READ MORE

answered Sep 27, 2018 in Java by anto.trigg4
• 3,420 points
112 views
0 votes
1 answer

Error - trustAnchors parameter must be non-empty

This bizarre message means that the truststore ...READ MORE

answered Oct 29, 2018 in Java by Daisy
• 8,050 points
77 views
+1 vote
2 answers
0 votes
2 answers
0 votes
1 answer

Why “non-static method cannot be referenced from a static context”?

You can't call something that doesn't exist. ...READ MORE

answered Oct 17, 2018 in Java by sharth
• 3,330 points
186 views
0 votes
2 answers

Why it is not possible to define a static method in a Java interface?

Interfaces are concerned with polymorphism which is ...READ MORE

answered Aug 27 in Java by Sirajul
• 29,340 points
81 views
0 votes
1 answer

Can a static reference be made to a non-static method

setLoanItem() isn't a static method, it's an instance ...READ MORE

answered Jun 26, 2018 in Java by samarth295
• 2,190 points
75 views
0 votes
1 answer

What is the simplest way to read JSON from a URL in java

Read json from url use url.openStream() and read contents ...READ MORE

answered Jun 13, 2018 in Java by samarth295
• 2,190 points
288 views