2s
Need help with this lesson?
📚AP Computer Science A•easy• 8 min

Calling Methods

Invoke both void and value-returning methods on objects, pass arguments, and utilize returned results within larger expressions.

A method is a named action. You call a method on an object (or on a class) using the dot operator. Methods are how you make objects do useful work, and how you read information back out of them.

void vs. value-returning methods

A void method does something but hands nothing back — System.out.println(...) prints and returns nothing. A value-returning method hands a value back that you can store or use in an expression.

System.out.println("hi");      // void: prints, returns nothing
int len = "hello".length();    // returns 5; we store it
System.out.println(len);

Output:

hi
5

Parameters and arguments

A method may need information, listed as parameters in its header. The actual values you pass are arguments. Order and type must match the header.

String s = "computer";
String part = s.substring(0, 4);  // arguments 0 and 4
System.out.println(part);

Output:

comp

Instance methods vs. static (class) methods

An instance method is called on an object: s.length(). A static method belongs to the class itself and is called on the class name: Math.sqrt(16). The thing before the dot tells you which kind it is.

[Diagram — see the figure in the print workbook.]

Worked example 1

A void method call.

System.out.print("A");
System.out.print("B");
System.out.println("C");

Output:

ABC

print does not add a new line; println does. All three append to the same line, then the last one ends it.

Worked example 2

Store a returned value.

String word = "banana";
int n = word.length();
System.out.println(n);

Output:

6

length() returns an int. We store it in n and print it. “banana” has 6 characters.

Worked example 3

Pass arguments that match the header.

String s = "mississippi";
System.out.println(s.indexOf("ss"));

Output:

2

indexOf(String) returns the index of the first match, or −1.

The first “ss” begins at index 2 (m=0, i=1, s=2).

Worked example 4

Static method on a class.

System.out.println(Math.max(8, 3));
System.out.println(Math.abs(-12));

Output:

8
12

Math methods are static, so we call them on the class name Math, not on an object.

Worked example 5

The return-value trap.

String s = "quiet";
s.toUpperCase();
System.out.println(s);

Output:

quiet

toUpperCase() returns a new String but we ignored it, and Strings are immutable, so s is unchanged. To capture it: s = s.toUpperCase();.

Common trap: If it returns a value, you must catch it

Many String methods return a new value and leave the original unchanged. Writing s.toUpperCase(); on its own line does nothing useful — the upper-case result is thrown away. You must use it: s = s.toUpperCase(); or print/store the result.

Hack: Read the header like a contract

Every method header tells you three things: what comes before the dot (an object or a class), what goes inside the parentheses (parameters, in order), and what comes out (the return type, or void). If the return type is not void, plan to store or use the result.

Quick test for “does this method change the object?” — if it returns a value and the class is immutable (like String), it does not change the original; it gives you a new one.

Print companion
AP Computer Science A Power Workbook
Off-screen practice on Amazon