void Keyword Usage and Examples
Learn about the Java void keyword, when and how to use it in methods, and explore real-world examples with detailed explanations. Ideal for Java beginners.
This reference explains void Keyword Usage and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
void Keyword in Java
In Java, the void keyword is used to indicate that a method does not return any value. It's a fundamental part of method declarations, especially when you want a method to perform an action (like printing, updating, or processing something) but not send a result back to the caller.
Why Do We Use void in Java?
Think of void as your way of saying: "Hey, this method is going to do something, but you don’t need to expect a result from it." It's a design choice that keeps your code clean, organized, and intention-driven.
Syntax of a void Method
accessModifier void methodName() {
// method body
}
Example 1: A Simple void Method
public class HelloWorld {
public static void greet() {
System.out.println("Hello, world!");
}
public static void main(String[] args) {
greet(); // Calling the method
}
}
Hello, world!
Here, the method greet() does not return anything. It simply performs an action — printing a message.
Example 2: void Method with Parameters
public class MathUtils {
public static void printSum(int a, int b) {
System.out.println("Sum: " + (a + b));
}
public static void main(String[] args) {
printSum(5, 7);
}
}
Sum: 12
This method accepts parameters and performs a calculation, but since it’s declared with void, it doesn’t return the result — it just prints it.
Can a void Method Use return ?
Yes, but with a twist. In a void method, you can use the return; statement only to exit early — not to return a value.
public class ConditionalReturn {
public static void checkAge(int age) {
if (age < 18) {
System.out.println("You are underage.");
return;
}
System.out.println("You are eligible.");
}
public static void main(String[] args) {
checkAge(15);
checkAge(21);
}
}
You are underage.
You are eligible.
Notice how return; exits the method early without returning any data. This is useful for breaking out of the method under specific conditions.
What Happens If You Try to Return a Value in a void Method?
Java won’t allow it. You’ll get a compile-time error like this:
public static void brokenMethod() {
return 42; // ERROR! Can't return a value from a void method.
}
Error Message
error: incompatible types: unexpected return value
void vs return Type Comparison
| Aspect | void Method | Method with Return Type |
|---|---|---|
| Returns a value? | No | Yes |
| Used for actions like printing/logging? | Yes | Sometimes, but usually for computations |
Can use return;? |
Yes (without value) | Yes (with value) |
| Example | void show() |
int add(int a, int b) |
Best Practices for Using void
Java · example discussed here
public static void brokenMethod() {
return 42; // ERROR! Can't return a value from a void method.
}
- Use
voidwhen your method performs an operation but doesn't need to pass data back. - Avoid making methods
voidif you’re relying on their output for further processing. - Keep
voidmethods focused on side effects like printing, logging, or updating UI or state.
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.