Reputation: 109
import java.awt.Rectangle;
public class Rec {
public static void main(String[] args) {
Rectangle r1;
r1 = new Rectangle(2, 5, 15, 15);
System.out.println(r1.getHeight);
}
}
and i get an error like:
System.out.println(r1.getHeight);
^
symbol: variable getHeight
location: variable r1 of type Rectangle
1 error
I don't get what is wrong with this code, I'm new user so it may be simple but I couldn't find the problem :(
Upvotes: 0
Views: 1511
Reputation: 1500
Use r1.getHeight()
instead of r1.getHeight
because getHeight();
definded in Rectangle class . use for Gets the bounding Rectangle of this Rectangle. Link
Upvotes: 0
Reputation: 35950
To invoke a Java method (and Rectangle.getHeight() is a method), you need a parentheses, like:
System.out.println(r1.getHeight());
By the way, for a method that accepts arguments, you would put those arguments in between parentheses:
// This is a method declaration. It says it returns an integer
// and it accepts two integers as its arguments.
int addTwoNumbers(int a, int b) {
return a + b;
}
// Somewhere in the code, you could call this method by passing
// two integers in there:
int number1 = 1;
int number2 = 10;
int result = someobject.addTwoNumbers(a, b);
// result is now 11.
Upvotes: 2