Programmers learn & share
0 votes

Problem :

Getting bellow error
"non static method cannot be referenced from a static context java"
by (6.9k points)   | 66 views

1 Answer

0 votes

Solution :

One can not make the reference to a Static variable from a NonStatic method. 

Static Variables are called class variables as they belong to the class and they have only one instance which is created at the begining only. 
NonStatic Variables need to be initialized every time one creates an object of that Class.

When one uses new() keyword to create a copy of every NonStatic filed for every object, but it is not the case for Static variables. 
So it gives compile time error if you are refering a Static variable from a NonStatic method.
by (36.1k points)  
2,183 questions
2,484 answers
241 users