. Advertisement .
..3..
. Advertisement .
..4..
Getting the non-static method cannot be referenced from a static context error while entering a String value from an R.string resource XML file? We got your back right here!
Scroll down to grasp more on causes and how to resolve such a problem.
Why Does The Non-Static Variable Cannot Be Referenced From A Static Context Error Occur In Java?
Most novice Java programmers get the message non static variable cannot be referenced from a static context while compiling their programs.
As the name refers, the main cause of the problem is because they employ a non-static member variable in the main() function. But why so, anyway?
In fact, we don’t need to build an object to use the main() function since this feature has been automatically called as a static method in Java. Have a look at the example belows so you can gain a more depicted picture about it!
Running the code:
package rollbar;
public class StaticContextVariable {
private int count = 0;
public static void main(String... args) {
count++;
System.out.println(count);
}
}
Output:
StaticContextVariable.java:8: error: non-static variable count cannot be referenced from a static context
count++;
^
StaticContextVariable.java:9: error: non-static variable count cannot be referenced from a static context
System.out.println(count);
^
2 errors
It turns out that by simply grasping Java’s static and non-static methods, you are one step closer to finding ways out of trouble. So, wait no longer but let’s see what they are!
- Static Method:
A static method is a method that belongs to a class but is not associated with an instance of that class and may be invoked without the presence of the class’s instance or object.
Only static data members and static methods of the same class or another class are accessible in a static method. Non-static methods and variables are inaccessible.
- Non-static method:
A non-static method is one whose definition lacks the static keyword.
In a non-static method, the method has access to both static and non-static members and methods of the same or another class, as well as the ability to alter the values of any static data member.
How To Fix The Non-Static Variable Cannot Be Referenced From A Static Context Error?
The non-static method cannot be referenced from a static context issue has a vastly straightforward solution.
As such, we must use the object name to reference the non-static variable in the code above. To refer to a non-static variable from a static environment, we need to build an object of the class.
Each time a new instance of a variable is produced, a new copy of every non-static variable is also created. So, using the reference of the new class instance, we can access these variables.
Take the following Java application, for instance.
class staticExample {
int number = 0;
public static void main(String args[])
{
// using non-static variable by creating an object of the class
staticExample test = new staticExample();
test.number++;
System.out.println(test.number);
}
}
Output:
C:\Users\ajeet\OneDrive\Desktop\programs>javc StaticExample4.Java
C:\Users\ajeet\OneDrive\Desktop\programs>jav StaticExample
C:\Users\ajeet\OneDrive\Desktop\programs>
The Bottom Line
This article hopefully can bring forth to you some helpful insight regarding the non-static method cannot be referenced from a static context error. If you have any concern not yet resolved, hesitate no more but let us know so we can assist you in fixing them. See then!
Leave a comment