why the i m getting the error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

why the i m getting the error

public class Axis { public static void main(String[]args) { Fire high = new Fire(); Fire.setH(23); int j = Fire.getH(); System.out.print(j); } } class Fire { private static int h; public void setH(int y) { this.h=y; } public static int getH() { return h; } } ERROR: non static method setH(23)cannot be refrenced through static context.

2nd Apr 2017, 6:00 AM
shobhit
shobhit - avatar
9 Answers
+ 1
Good job. Because you made setH static method , and h is static , then you can't use (this ) * this is a non static variable To solve this problem and make the program run well, instead of it just write (h=y;) in setH method
2nd Apr 2017, 8:37 PM
Hazem Twair
Hazem Twair - avatar
+ 17
public class Vehicle { private String color; // Getter public String getColor() { return color; } // Setter public void setColor(String c) { this.color = c; } } //Main class Program { public static void main(String[ ] args) { Vehicle v1 = new Vehicle(); v1.setColor("Red"); System.out.println(v1.getColor()); } } //Order out of Khaos ...👍
2nd Apr 2017, 6:11 AM
Cory Gutierrez🔹
Cory Gutierrez🔹 - avatar
+ 3
high.setH(23); instead of Fire.setH(23); EDIT: A non-static class (being instantiated by You anyway ) can be accessed by instance - my first line. A static class is reachable by referrence only- my second line.
2nd Apr 2017, 6:35 AM
Michał Bujakowski
Michał Bujakowski - avatar
+ 3
@ Twair. i m still getting error
2nd Apr 2017, 9:56 AM
shobhit
shobhit - avatar
+ 3
https://code.sololearn.com/c310PzMN7ngl/?ref=app. I have provided link thnx for your response.
2nd Apr 2017, 2:15 PM
shobhit
shobhit - avatar
+ 2
use public static void setH() or call with the object not with class directly
2nd Apr 2017, 9:23 AM
sarvagya vishwakarma
sarvagya vishwakarma - avatar
+ 2
If you want to call with the object you should remove static word from all methods in class Fire. And call like (high.setH(3), high. getH()) Otherwise , you should make all methods static. And call like (Fire. setH(4), Fire. getH())
2nd Apr 2017, 9:36 AM
Hazem Twair
Hazem Twair - avatar
0
shobhit , could write here the new code you have just edited?
2nd Apr 2017, 10:03 AM
Hazem Twair
Hazem Twair - avatar
- 1
The above code that you wrote in first time (your question) can be solved by just writing (high. setH(23) ) instead of (Fire. setH(23)) as Michal Bujakowski said
2nd Apr 2017, 1:01 PM
Hazem Twair
Hazem Twair - avatar