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

+3 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 26, 2018 in Java by Parth
• 4,640 points
249,903 views
Can someone explain how these tutorials will work then? I am a newbie to java and static vs non static.

I am trying to add a JLIST to a form create with the GUI designer built into Intellji and for the life of me I cannot get JLIST to work.

Hi, @Jeranio,

static method is a method that belongs to a class, but it does not belong to an instance of that class and this method can be called without the instance or object of that class.

Every method in java is a non-static method, but the methods must not have a static keyword before the method name. non-static methods can access any static method and static variable also, without using the object of the class.

Regarding adding a JLIST to a form created with the GUI designer. I would suggest you visit here, as you have to follow multiple steps.

14 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,220 points
no.............
this is clear and helpful, thank you
And also very wrong

"To understand this, you need to understand the difference between static and non-static."

So do all C programmers not understand the difference between static and non-static??

Hi, @There,

I guess this would be enough for anyone to understand the difference between static and non-static.

static variable acts as a global variable and is shared among all the objects of the class. Non-static variables are specific to instance object in which they are created.

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 30, 2019 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, 2019 by Neha
• 330 points
Thanks for the explanation @Neha! Can you also mention how to solve this error please?
0 votes

Hey all,

Even I have faced the same scenario a little before, “A Non-static method cannot be referenced from a static context” error, A reference cannot be made from a static to a non-static method. ... Static variables are class variables that belong to the class with only one instance created initially.

But, to access instance variables it is a must to create an object, these are not available in the memory, before instantiation. Therefore, you cannot make a static reference to non-static fields(variables) in Java.

answered Dec 10, 2020 by Rajiv
• 8,870 points
0 votes

Hey Techies,

Non-static variables are part of the objects themselves. To use a non-static variable, you need to specify which instance of the class the variable belongs to. ... In other words, non-static data cannot be used in static methods because there is no well-defined variable to operate on.

answered Dec 10, 2020 by Roshni
• 10,480 points

Related Questions In Java

0 votes
1 answer

Error: setOnItemClickListener cannot be used with a spinner

Hello @kartik, See the first line of your ...READ MORE

answered Jun 1, 2020 in Java by Niroj
• 82,840 points
3,676 views
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,920 points
1,113 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, 2019 in Java by Karan
• 19,610 points
9,222 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 28, 2018 in Java by anto.trigg4
• 3,440 points
1,283 views
+1 vote
2 answers
0 votes
2 answers

Why are you not able to declare a class as static in Java?

A static keyword  can be used with ...READ MORE

answered Jun 11, 2019 in Java by Neha
• 330 points
1,529 views
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,370 points
1,211 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, 2019 in Java by Sirajul
• 59,230 points
2,105 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,220 points
921 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,220 points
5,212 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP