Calling Methods

Calling a method in an object is similar to referring to its instance variables: Dot notation is used. The object whose method you're calling is on the left side of the dot, and the name of the method and its arguments are on the right side of the dot:

myCustomer.addToOrder(itemNumber, price, quantity);

Note that all methods must have parentheses after them, even if the method takes no arguments:

myCustomer.cancelAllOrders();

Listing 4.3 shows an example of calling some methods defined in the String class. Strings include methods for string tests and modification, similar to what you would expect in a string library in other languages.

Listing 4.3 The Full Text of CheckString.java
 1: class CheckString { 2: 3: public static ...

Get Sams Teach Yourself Java 2 in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.