Possible Duplicate:
What is the reason behind “non-static method cannot be referenced from a static context”?
Cannot make a static reference to the non-static method
cannot make a static reference to the non-static field
I am not able to understand what is wrong with my code.
class Two {
public static void main(String[] args) {
int x = 0;
System.out.println("x = " + x);
x = fxn(x);
System.out.println("x = " + x);
}
int fxn(int y) {
y = 5;
return y;
}
}
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static method fxn(int) from the type Two
Since the
mainmethod isstaticand thefxn()method is not, you can’t call the method without first creating aTwoobject. So either you change the method to:or change the code in
mainto:Read more on
statichere in the Java Tutorials.