Core Java

What does void mean in Java?

In this post, we feature a comprehensive article explaining what Void means in Java.

1. What does void mean in Java?

What does void mean in Java

In Java, void keyword is used with the method declaration to specify that this particular method is not going to return any value after completing its execution. We cant assign the return type of a void method to any variable because void is not a data type.

2. Example

Let’s see an example of void keyword usage.

VoidExample.java

package example.javaCodeGeeks;

public class VoidExample {

	public static void main(String[] args) {
		voidMethod();
		String returnedString = returningMethod();
		System.out.println(returnedString);
	}

	public static void voidMethod() {
		System.out.println("voidMethod is called");
	}

	public static String returningMethod() {
		return "returningMethod is called";
	}
}

Output

voidMethod is called
returningMethod is called

In the above example, we have two methods voidMethod with void as return type and returningMethod with String as return type. If we try to assign the return value of voidExample to any datatype it will give us the compile-time error. when voidMethod is called, the control simply goes to the calling method which is main method after completing its task.

Even though void method doesn’t return any value still, we can use return statement in void method when we want to stop the further execution.

Let’s see an example demonstrating the usage of the return keyword with a void method.

VoidWithReturnExample .java

package example.javaCodeGeeks;

public class VoidWithReturnExample {
	public static void main(String[] args) {
		Person person1 = new Person();
		person1.setName("Ben");
		person1.setAge(-5);
		System.out.println(person1);
		Person person2 = new Person();
		person2.setName("Tony");
		person2.setAge(17);
		System.out.println(person2);
	}
}

class Person {
	// default age
	int age = 1;
	String name;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		if (age < 1)
			return;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return name + " is " + age + " year old.";
	}
}

Output

Ben is 1 year old.
Tony is 17 year old.

Here as we can see in the method setAge of class Person, if the age is smaller than 1 the method will simply return the control to the caller, without executing the next statement.

3. When to use void method and method with return type

The most common use of void method in java is when we want to change the internal state of the object but do not require the updated state. For example in VoidWithReturnExample.java , the method setName and setAge are only used to change the name and age respectively, but they don’t return anything.

On the other hand, we use the method with return type when we require the result of some calculation or some value in return. For example, in VoidWithReturnExample.java the getter methods of the Person class are used to return the state of the object. In the same example in Person class we have toString method which computes a String using the name and age field and return the result, which is called when we print the Person class object.

4. Download the source code

Download
You can download the full source code of this example here: What does void mean in Java?

Yusuf Ansari

I am Yusuf Ansari, I earned my degree in Information Technology from Guru Gobind Singh Indraprastha University. I have over two years of professional experience in Java based application development.I have worked as a backend developer mostly but also worked on the front-end part in some React.js based projects.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button