+ 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.
9 ответов
+ 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
+ 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 ...👍
+ 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.
+ 3
@ Twair. i m still getting error
+ 3
https://code.sololearn.com/c310PzMN7ngl/?ref=app.
I have provided link thnx for your response.
+ 2
use public static void setH()
or call with the object not with class directly
+ 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())
0
shobhit , could write here the new code you have just edited?
- 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