Reputation:
I have two seperate classes, one where i actually do the calculation of the area of a square like the first bit below, and one where I display the result but the one were i display the result is giving me an error saying that it can't find the "computeSquareArea" symbol. Why is it doing this and not transferring between classes?
public double computeSquareArea(double length)
{
return length * length;
}
double sqArea = computeSquareArea(length);
System.out.println("Area of the square is " + sqArea);
Upvotes: 0
Views: 34
Reputation: 1806
As I can see, your method computeSquareArea(double length)
is not static. So I guess you need to Intantiate
the class that have your method computeSquareArea(double length)
. Like for Example, lets assume your Class Name is SomeClass
.
SomeClass someClass = new SomeClass();
double sqArea = someClass.computeSquareArea(length); // This would be the method of your newly created Instance of SomeClass
System.out.println("Area of the square is " + sqArea);
Upvotes: 1
Reputation: 744
Non-static methods belong to the instance of the object, while static methods belong to the class.
In your code, computeSquareArea()
is a non-static method. This means that in order to call it, you must call it on an instance of the class that it is in.
If you want to be able to call the method without requiring an instance first, you can use a static method. In order to make the method static, change public double computeSquareDistance()
to public static double computeSquareDistance()
You can read more about this here.
Upvotes: 1