Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
Java keywords

void Keyword in Java

The void keyword in Java is used to specify that a method does not return any value. It is a return type that indicates the method performs an action but does not produce a result that can be used elsewhere in the code.

Usage

The void keyword is typically used in method declarations. When a method is declared with a void return type, it means that the method will not return any value.

Syntax

void methodName(parameters) {
    // method body
}
  • methodName: The name of the method.
  • parameters: The parameters required by the method (if any).

Examples

Example 1: Basic Void Method

public class VoidExample {
    public static void main(String[] args) {
        printMessage();
    }

    public static void printMessage() {
        System.out.println("Hello, World!");
    }
}

In this example, the printMessage method is declared with a void return type. It simply prints "Hello, World!" to the console and does not return any value.

Example 2: Void Method with Parameters

public class Greeting {
    public static void main(String[] args) {
        greet("Alice");
    }

    public static void greet(String name) {
        System.out.println("Hello, " + name + "!");
    }
}

Here, the greet method takes a String parameter name and prints a greeting message. The method is declared with a void return type, indicating it does not return any value.

Example 3: Void Method in a Class

public class Counter {
    private int count = 0;

    public void increment() {
        count++;
    }

    public void displayCount() {
        System.out.println("Count: " + count);
    }

    public static void main(String[] args) {
        Counter counter = new Counter();
        counter.increment();
        counter.displayCount();
    }
}

This example defines a Counter class with two void methods: increment and displayCount. The increment method increments the count variable, and the displayCount method prints the current count. Neither method returns a value.

Tips and Best Practices

  • Use Descriptive Names: When defining void methods, use descriptive names that clearly indicate the action the method performs.
    public void calculateSum() {
        // method body
    }
  • Side Effects: Ensure that void methods are used for actions that have side effects, such as modifying object states, printing to the console, or writing to a file.
  • Avoid Return Statements: Do not use return statements with a value in void methods. A plain return; can be used to exit the method early if needed.
    public void checkCondition(boolean condition) {
        if (!condition) {
            return; // exit method early
        }
        // method body
    }
  • Method Overloading: You can overload void methods with other methods that have the same name but different parameter lists.
    public void display() {
        System.out.println("No parameters");
    }
    
    public void display(String message) {
        System.out.println(message);
    }
  • Documentation: Always document the purpose and behavior of void methods using comments or Javadoc to ensure clarity and maintainability.
    /**
     * Prints a welcome message to the console.
     */
    public void printWelcome() {
        System.out.println("Welcome!");
    }
  • Void Class: In Java, the Void class is an uninstantiable placeholder class to hold a reference to the void keyword. This class is part of the java.lang package.
    public final class Void {
        public static final Class<Void> TYPE = (Class<Void>) Class.getPrimitiveClass("void");
    }
  • Main Method: The public static void main(String[] args) method is the entry point of any Java application. The void keyword here signifies that this method does not return any value.
    public static void main(String[] args) {
        // application code
    }