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

in Java
Java Tutorials
1 Answers to this question

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 Certification Training page for more information, where you can find the       and    Java Interview Questions FAQ's and answers    as well.

For more updates on the latest courses stay tuned to HKR Trainings.

To Top