W3Schools Java Void Tutorial Mastery Complete Guide

Robert Jul 13, 2026

Java is a powerful programming language known for its versatility and wide range of applications. One fundamental concept that Java developers often work with is the `void` keyword. Let's delve into what `void` means in Java and its significance in various contexts.

Public Static Void Main Java Sticker
Public Static Void Main Java Sticker

At its core, the `void` keyword in Java signifies the absence of a value or outcome. It is used in situations where a method does not return any value, or when dealing with empty spaces. Understanding `void` is vital for any Java developer, as it forms the backbone of many operations and structures.

Java Programming Language
Java Programming Language

The `void` Keyword in Java Methods

In Java, the `void` keyword is primarily used to indicate that a method does not return any value. This concept is crucial for methods that are used to perform an action but don't have an outcome that needs to be captured or returned.

a computer screen with some type of text on it and numbers in the bottom left corner
a computer screen with some type of text on it and numbers in the bottom left corner

A method declared with the `void` keyword can't be used where a value is expected. It is typically used in methods that are called to trigger a specific action, such as displaying output, rather than performing a calculation and returning a result.

`void` Main Method

Java Variables: The Building Blocks of Programming
Java Variables: The Building Blocks of Programming

The `main` method in Java is declared with the `void` keyword, illustrating its purpose. This method is the entry point of any Java application, and it's where the program starts execution. Since it doesn't return any value, its return type is `void`.

Here's a simple example of a `main` method that uses `void`: ```java public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } } ```

Void Methods with Parameters

a black and white graduation hat holding a red book with a cup of coffee in it
a black and white graduation hat holding a red book with a cup of coffee in it

Methods with the `void` return type can also accept parameters, allowing them to perform actions based on the provided inputs. These methods don't return a value, but they can have an impact on the program's state or behavior.

For instance, a method can be created to sort an array in-place without returning any value: ```java public class ArrayUtils { public static void sort(int[] array) { // Sorting logic here } } ```

The `void` Keyword in Java Generics

JAVA MAIN METHOD
JAVA MAIN METHOD

In Java Generics, `void` can also be used to represent the absence of a type argument. When creating a generic class or method that does not accept any type argument, `void` can be used in the type parameter section.

Here's an example of a generic `Pair` class where one element's type is not specified (`void`): ```java public class Pair { private T first; private void second; public Pair(T first, void second) { this.first = first; this.second = second; } // getters and setters } ```

Top Features of Java Every Beginner Should Know β˜•πŸš€
Top Features of Java Every Beginner Should Know β˜•πŸš€
Java OOPs Concepts - Object Oriented Programming in Java - TechVidvan
Java OOPs Concepts - Object Oriented Programming in Java - TechVidvan
a red poster with the words sintaxis basica de java
a red poster with the words sintaxis basica de java
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
60 Java projects ideasπŸ’‘
60 Java projects ideasπŸ’‘
Void(0) in JavaScript: Full Guide
Void(0) in JavaScript: Full Guide
How to Become a Java Developer (Step-by-Step)
How to Become a Java Developer (Step-by-Step)
core java classes in Pune
core java classes in Pune
Learn Java in 30 Days Only
Learn Java in 30 Days Only
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet πŸŽ―πŸ’»
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet πŸŽ―πŸ’»
Java OOPs Concept
Java OOPs Concept
Best Java IDEs in 2020
Best Java IDEs in 2020
Java Cheatsheet
Java Cheatsheet
Web Application, 10 Things
Web Application, 10 Things
Java Project Ideas For Beginners
Java Project Ideas For Beginners
πŸš€ 5 Essential Java Methods Every Developer Should Know! πŸ’»
πŸš€ 5 Essential Java Methods Every Developer Should Know! πŸ’»
Level Up Your Java πŸš€ Intermediate Cheat Sheet You’ll Actually Use
Level Up Your Java πŸš€ Intermediate Cheat Sheet You’ll Actually Use
Learn Java in 45 Days
Learn Java in 45 Days
JavaScript Basics You MUST Know
JavaScript Basics You MUST Know
Must-Have IntelliJ IDEA Plugins For Java Developers
Must-Have IntelliJ IDEA Plugins For Java Developers

`Pair` Class Example

In this `Pair` class, the `second` field is typed with `void`. This means that this field can hold any value, regardless of its type. This can be useful when you want to create a pair with the first element being of a specific type, but the second element being of no particular type.

It's worth noting that using `void` in Java Generics is not very common. This is because `void` is typically used to indicate the absence of a value, while in generics, the focus is on the presence and type of values.

Understanding the `void` keyword in Java is essential for writing effective, functional code. Whether it's indicating that a method has no return value or using it in generics to represent the lack of a type argument, `void` plays a crucial role in Java programming. Familiarizing yourself with `void` will help you build robust and efficient Java applications.