• Register
0 votes
98 views

Problem :

Getting bellow error
"non static method cannot be referenced from a static context java"
6 5 3
6,930 points

Please log in or register to answer this question.

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.
9 7 4
38,600 points

Related questions

1 vote
1 answer 26 views
26 views
Problem: I am very noob in java programming. I was doing my coding practice and wrote this code: public class StaticDemo { int i = 0; public static void main(String[] args) { System.out.println("in main method which is static"); // Trying to access ... -static method instanceMethod() cannot be referenced from a static context instanceMethod(); ^ 3 errors Can anyone help me out to fix this problem?
asked Apr 7, 2020 LizzyM 6.1k points
1 vote
1 answer 4 views
4 views
Problem: Can someone please help fix this uncaught error : java non static method cannot be referenced from a static context
asked Mar 26 MUHAMMAD MUNEEB 80k points
1 vote
1 answer 2 views
2 views
Problem: How can I deal with this problem : java non-static method cannot be referenced from a static context
asked Mar 26 MUHAMMAD MUNEEB 80k points
0 votes
1 answer 17 views
17 views
Why? I am not asking for solutions.
asked Jan 3 TeamScript 13.5k points
0 votes
1 answer 2 views
2 views
Problem: I am stuck during learning as I have already mentioned in my question .someone please cooperate me to continue my project:main.this cannot be referenced from a static context.Thanks
asked 2 days ago ashik 10.7k points
1 vote
1 answer 5 views
5 views
Problem: I am unable to find out the solution, Please solve it : java non static method cannot be referenced from static context
asked Mar 26 MUHAMMAD MUNEEB 80k points
0 votes
1 answer 10 views
10 views
solve this question.
asked Jan 20 TeamScript 13.5k points
1 vote
1 answer 3 views
3 views
Problem; Getting the following error. Java non static variable this cannot be referenced from a static context.
asked Mar 29 PkGuy 13.1k points
1 vote
1 answer 4 views
4 views
Problem: Why I got the error : java non static variable cannot be referenced from a static context
asked Mar 26 MUHAMMAD MUNEEB 80k points
0 votes
1 answer 25 views
25 views
How do I get my methods to recognize my class variables?
asked Dec 28, 2020 TeamScript 13.5k points