01 / 20

Calling Class Methods

Learn how to invoke methods on objects and classes, pass arguments, and use return values in Java.

AP Computer Science A

What is a Method?

A method is a named block of code that belongs to a class. It performs a specific task and can be called (invoked) whenever you need that task done. Methods help organize code, avoid repetition, and make programs easier to read.

Behavior
A method defines what an object can do: actions like move, calculate, or print.
Reuse
Write the logic once, then call it as many times as needed without copy-pasting.

Anatomy of a Method Call

myDog.bark(3 Object Reference Dot Operator Method Name Argument (Parameter) Semi- colon objectReference.methodName(arguments);

Void vs. Return Methods

Every method has a return type. This tells Java whether the method sends a value back to the caller or simply performs an action.

void Methods
Perform an action but return nothing. Called as standalone statements.
System.out.println("Hi");
Non-void Methods
Compute and return a value. The result can be stored, printed, or used in an expression.
int len = str.length();

How a Method Call Flows

Caller Code int x = obj.add(3, 5); args passed Method Body int add(int a, int b) { return a + b; returns 8 Result x now holds 8 1 Call is made 2 Body executes 3 Value returned

Static vs. Non-Static Methods

A key distinction in Java: some methods belong to the class itself (static), while others belong to an individual object (non-static / instance methods).

Static Methods Non-Static Methods
Called on The class name An object reference
Syntax Math.sqrt(25) str.length()
Keyword static in header No static keyword
Access to Only static fields Instance + static fields
Needs object? No Yes

Parameters and Arguments

Parameter
The variable in the method header that receives data. It is a placeholder defined when the method is written.
Argument
The actual value passed when calling the method. It fills in the parameter at runtime.
// "int n" is the PARAMETER public void repeat(int n) { ... } // 5 is the ARGUMENT obj.repeat(5);

How to Call a Method

1
Identify the method: Check the class API or documentation for the method name, parameters, and return type.
2
Get a reference: For non-static methods, create an object or use an existing reference. For static methods, use the class name.
3
Use the dot operator: Write reference.methodName() or ClassName.methodName().
4
Pass arguments: Supply values inside the parentheses matching the parameter list.
5
Handle the return: If non-void, store the result in a variable, print it, or use it in an expression.

Example 1: Calling an Instance Method

public class Dog { private String name; public Dog(String n) { name = n; } public String getName() { return name; } }
Dog myDog = new Dog("Buddy"); String s = myDog.getName(); // returns "Buddy" System.out.println(s); // prints: Buddy

Example 2: Static Method Call

double result = Math.pow(2, 10); // result = 1024.0 int absolute = Math.abs(-42); // absolute = 42 double root = Math.sqrt(144); // root = 12.0

Example 3: Nesting Method Calls

Because non-void methods return values, you can use a method call anywhere a value is expected: as an argument, in a print, or in an expression.

String msg = "Hello, World!"; // Nested: length() result used as argument to println() System.out.println(msg.length()); // prints: 13 // Chained: substring returns a String, then toUpperCase is called on it String sub = msg.substring(0, 5).toUpperCase(); // sub = "HELLO" // Used in expression int half = msg.length() / 2; // half = 6

Calling Methods with this

Inside a class, one method can call another method on the same object using the implicit this reference, or simply by name (Java fills in this automatically).

public class Circle { private double radius; public double getArea() { return Math.PI * radius * radius; } public String toString() { // Calls getArea() on THIS object return "Area: " + getArea(); } }

Method Overloading

Java allows multiple methods with the same name in a class, as long as their parameter lists differ. Java picks the right version based on the arguments you pass.

public class Printer { public void print(String s) { ... } public void print(int n) { ... } public void print(String s, int n) { ... } } Printer p = new Printer(); p.print("Hi"); // calls print(String) p.print(42); // calls print(int) p.print("Hi", 3); // calls print(String, int)

Common Mistakes

✕ Forgetting Parentheses
Writing obj.getName instead of obj.getName(). Without (), Java sees a field reference, not a method call.
✕ Wrong Argument Types
Passing a String where an int is expected. The compiler will reject mismatched types.
✕ Ignoring Return Values
Calling a non-void method without storing or using the result: the returned value is simply lost.
✕ Static on Object
Calling a static method on an object like obj.sqrt(4) instead of Math.sqrt(4). It compiles but is misleading.

Summary of Method Calls

Concept Syntax / Rule Example
Instance call object.method(args) str.length()
Static call ClassName.method(args) Math.abs(-5)
void method Standalone statement list.add("x");
Non-void method Use return value int n = s.length();
Overloaded Same name, different params print("Hi") / print(5)
Nested call Inner evaluates first println(s.length())

Question 1 of 5

Which of the following correctly calls the static method Math.max to find the larger of two integers?

Question 2 of 5

Given the code below, what is printed?

String word = "computer"; int x = word.length(); System.out.println(x);

Question 3 of 5

What is the difference between a parameter and an argument?

Question 4 of 5

What does the following code output?

String s = "AP Java"; String t = s.substring(3); System.out.println(t.toUpperCase());

Question 5 of 5

A class has the following overloaded methods:

public void display(int x) { ... } public void display(String s) { ... } public void display(int x, int y) { ... }

Which call would cause a compile error?