Calling an Object's Methods
by, 04-26-2012 at 05:09 PM (503 Views)
Object reference can be used for invoking the object method. Simple names of method are appended to the object reference along with (.) dot operator. Within enclosing parentheses arguments are provided to the method. If any arguments are not required by the method empty parentheses shall be used.
Two methods are linked to Rectangle class: getArea() for the computation of the rectangular arear, & move() for chaning the origin of rectangle. CreateObjectDemo code is given to invoke 2 methods.Java Code:objectReference.methodName(argumentList); or objectReference.methodName();
First statement will be invoking rectOne getArea() method and results are displayed. The 2nd line will be moving rectTwo as move() provides new object values origin.x & origin.yJava Code:System.out.println("Area of rectOne: " + rectOne.getArea()); ... rectTwo.move(40, 72);