What Does Void Mean In Java? - Examples Java Code Geeks - 2022
Maybe your like
In this post, we feature a comprehensive article explaining what Void means in Java.
1. 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 calledIn 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
DownloadYou can download the full source code of this example here: What does void mean in Java?Do you want to know how to develop your skillset to become a Java Rockstar?Subscribe to our newsletter to start Rocking right now!To get you started we give you our best selling eBooks for FREE!1. JPA Mini Book2. JVM Troubleshooting Guide3. JUnit Tutorial for Unit Testing4. Java Annotations Tutorial5. Java Interview Questions6. Spring Interview Questions7. Android UI Designand many more ....I agree to the Terms and Privacy PolicySign up
Thank you!
We will contact you soon.
Tag » What Is Void In Java
-
Java Void Keyword - W3Schools
-
Java Programming/Keywords/void - Wikibooks, Open Books For An ...
-
Void Keyword Definition In Java - ThoughtCo
-
Void Type In Java | Baeldung
-
What Is A Void In Java? - Quora
-
Void (Java Platform SE 7 ) - Oracle Help Center
-
Void (Java Platform SE 8 ) - Oracle Help Center
-
Java Main() Method - Public Static Void Main(String[] Args)
-
자바에서 사용되는 Void란 무엇인가? - WEBISFREE
-
What Is The Purpose Of Void? - Stack Overflow
-
The Void Return Type In Java - YouTube
-
Java - Void Keyword - YouTube
-
Java Void Methods VS. Value Returning Methods - Appficial
-
The Void Keyword In Java Programming Language - Codeforcoding