Back to Blog
Java

Java Void Method: Syntax and Usage

java void method: Learn how to declare, call, and override void methods in Java, including early return behavior and common mistakes.

javavoidmethodsreturn-typemethod-signature
Diagram showing a Java void method call that performs an action without returning a value.

A Java void method is a method that performs work without returning a value. The void keyword appears in the method signature where the return type would normally be declared, and it tells the compiler that the method completes without producing a result. This is the most common way to model operations that have side effects, such as updating a database record, writing to a log, or mutating the state of an object.

What a Java void method is

The void keyword is not a type in the way that int or String are. It is a marker that indicates the absence of a return value. When a method declares void, the caller cannot use the method call as part of an expression, assign it to a variable, or pass it as an argument. The method exists solely for the work it performs during execution.

This distinction matters because Java is a strongly typed language. The compiler enforces the void contract at compile time. If you try to use the result of a void method, the code will not compile, which is preferable to discovering the problem at runtime.

Declaring a void method

The declaration follows the standard method syntax: access modifier, optional modifiers, return type, method name, and parameter list.

public class AuditService { public void recordEvent(String eventType, String actor) { // write the event to the audit log } }

The void keyword occupies the position where a type such as int or String would appear. Because the method has no return value, the body does not need a return statement. Execution reaches the end of the body and control returns to the caller automatically.

Calling a void method

A void method is invoked as a statement. The caller cannot assign the result to a variable because there is no result.

AuditService audit = new AuditService(); audit.recordEvent("LOGIN", "alice");

Attempting to write String result = audit.recordEvent(...); produces a compile-time error. The compiler reports that the method returns void, so it cannot be used in an expression. This is a common source of confusion for developers coming from languages where every function produces a value.

The return statement in a void method

A void method may contain a return statement, but it must be a bare return; without a value. The statement terminates the method immediately.

public void sendNotification(User user) { if (user == null || user.email() == null) { return; } emailService.send(user.email(), "Your account has been updated"); }

The bare return is useful for early exit when a precondition fails. It does not return a value; it simply stops execution. A return with a value, such as return user;, is a compile-time error inside a void method.

Void methods with parameters

A void method can accept any number of parameters, including none. The parameters are the inputs the method uses to perform its work.

public void updateInventory(String sku, int quantity) { InventoryItem item = inventory.find(sku); item.adjustStock(quantity); }

The absence of a return value does not mean that the method cannot affect the program. It can mutate objects passed as arguments, modify instance fields, or trigger external operations. The side effects are the reason the method exists.

Void in method overriding

When a subclass overrides a void method, the override must also declare void as its return type. Java does not allow the override to change the return type to a value type.

public interface Notifier { void notify(String message); } public class EmailNotifier implements Notifier { @Override public void notify(String message) { // send email } }

This constraint keeps the contract consistent. A caller that invokes the method through the interface or parent type expects no return value, and the implementation cannot introduce one. The same rule applies when a method is overridden in a subclass: the return type must remain void.

Common mistakes with void methods

The most frequent mistake is treating a void method as if it returned a value. This happens when a developer writes a method that computes a result but forgets to change the return type from void to the actual type.

public void calculateTotal(Order order) { return order.items().stream() .mapToDouble(Item::price) .sum(); // compile error }

The fix is to change the return type from void to double and return the computed value. Another mistake is returning null from a void method, which is also a compile-time error because null is a value. A related issue is declaring a method void when the caller genuinely needs the result, which forces the method to communicate its outcome through an out parameter or an exception, both of which are more awkward than simply returning the value.

Choosing between void and a return type

The decision depends on whether the caller needs the result of the operation. If the caller must know the outcome, such as whether a record was created or how many rows were affected, the method should return a value. If the operation is purely a side effect, such as logging or sending a notification, void is appropriate.

public void log(String message) { ... } // side effect only public boolean deleteUser(String id) { ... } // caller needs the outcome

Returning a value also makes testing easier because the caller can assert on the result rather than inspecting internal state. When in doubt, prefer returning a value if the result is meaningful; reserve void for operations where the result is genuinely irrelevant to the caller. This keeps the method contract explicit and avoids forcing callers to guess whether a side effect actually occurred.

java void method: Practical Usage and Code Examples | RYUSLOG DEV