Skip to main content
United StatesComputer ScienceSyllabus dot point

How do you call a void method on an object using dot notation, and what does void mean?

Topic 2.3 Calling a Void Method: call a non-static void method on an object using dot notation, and explain that a void method performs an action but returns no value.

A focused answer to AP CSA Topic 2.3, covering dot notation for calling methods on objects, what void means, why a void call cannot be used in an expression, the difference between a method signature and a call, and method side effects, with a worked example.

Generated by Claude Opus 4.88 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Dot notation
  3. What void means
  4. Signature versus call
  5. Try this

What this topic is asking

The College Board (Topic 2.3) wants you to call a non-static void method on an object using dot notation (object.method();), and to understand that a void method performs an action but returns no value, so its call cannot be used as a value in an expression. This is the simplest form of method call and the template for the parameter and return-value variants that follow.

Dot notation

For example, if account refers to a BankAccount object with a method printStatement, you call it with:

account.printStatement();

The object to the left of the dot is the one the method acts on. A non-static method must be called on an object; calling it on the class name does not work, because there is no specific object for it to act on.

What void means

A void method typically has a side effect: it changes the object's state (an attribute), prints output, or otherwise affects the program, rather than computing and handing back a result. System.out.println(...) is itself a void method - it displays text but gives you nothing back. Trying to write int x = lamp.turnOff(); is a compile-time error, because turnOff returns nothing to store.

Signature versus call

The class defines a method's signature - its name and the types of its parameters, for example void deposit(double amount). When you call the method, you supply arguments that match the parameter list. For a no-argument void method the parentheses are empty, but they are still required: omitting them (lamp.turnOff) is not a method call and will not compile.

Try this

Q1. Write a statement that calls a void method start() on an object referred to by engine. [1 point]

  • Cue. engine.start();

Q2. Explain why double d = pump.fill(); does not compile if fill is a void method. [2 points]

  • Cue. A void method returns no value, so there is nothing to assign to d; using a void call where a value is expected is a compile-time error.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2021 (style)1 marksMultiple choice. A class `Robot` has a void method `turnLeft()`. Given `Robot r = new Robot();`, which statement correctly calls the method? (A) `turnLeft(r);` (B) `r.turnLeft();` (C) `Robot.turnLeft();` (D) `int x = r.turnLeft();` (E) `r.turnLeft;`
Show worked answer →

The answer is (B).

A non-static method is called on an object using dot notation: the object reference, a dot, the method name, and parentheses for the (empty) argument list. (A) uses the wrong syntax and treats r as an argument. (C) tries to call it on the class, but the method is non-static. (D) tries to store a returned value, but a void method returns nothing, so this does not compile. (E) omits the parentheses, so it is not a method call.

Markers reward the dot-notation pattern object.method(); and knowing a void method returns no value to store.

AP 2019 (style)3 marksFree response (code writing). A class `Light` has void methods `turnOn()`, `turnOff()` and `dim()`. Write a code segment that creates a `Light` object named `lamp`, then turns it on, dims it, and turns it off, in that order, by calling the methods on the object.
Show worked answer →

A 3-point question testing void method calls with dot notation.

Light lamp = new Light();
lamp.turnOn();
lamp.dim();
lamp.turnOff();

Point 1: creating the Light object with new. Point 2: calling all three methods on lamp using dot notation with parentheses. Point 3: calling them in the required order (on, dim, off). Writing lamp.turnOn without parentheses, or trying to store a return value, would be wrong.

Related dot points

Sources & how we know this