Small Java Problem

CrackaLackaZe

Senior member
Jun 29, 2002
922
0
76
Whenever I try to compile the attached code, it gives me:

test2.java:45: non-static method getValue() cannot be referenced from a static context
numberz.setValue(numberz.getValue() + x);
^
test2.java:45: non-static method getValue() cannot be referenced from a static context
numberz.setValue(numberz.getValue() + x);
^
2 errors

Can anyone tell me why I'm getting that?
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
classname.methodname() is used to call static methods. to call non-static methods, call it without the "classname." part or use "this." instead of "classname."

so change numberz.setValue(numberz.getValue() + x); to

setValue(getValue() + x); OR this.setValue(this.getValue() + x); (both are equivalent)