I can not compile the following code:
public class Test { public static void main (String [] args ){ int a = calcArea(7, 12); System.out.println(a); } int calcArea(int height, int width) { return height * width; } }
The following error appears:
The non-static calcArea (int, int) method cannot be referenced from static content
What does it mean? How can I solve this problem ..?
EDIT:
Based on your advice, I made an instance that is new test () as follows:
public class Test { int num; public static void main (String [] args ){ Test a = new Test(); a.num = a.calcArea(7, 12); System.out.println(a.num); } int calcArea(int height, int width) { return height * width; } }
It is right? What difference does it make if I do this ...
public class Test { public static void main (String [] args ){ int a = calcArea(7, 12); System.out.println(a); } static int calcArea(int height, int width) { return height * width; } }
source share