Uses for the Java Void Reference Type?
Void
has become convention for a generic argument that you are not interested in. There is no reason why you should use any other non-instantiable type, such as System
.
It is also often used in for example Map
values (although Collections.newSetFromMap
uses Boolean
as maps don't have to accept null
values) and java.security.PrivilegedAction
.
On Void return type
Void
is a class like any other, so a function returning Void
has to return a reference (such as null
). In fact, Void
is final
and uninstantiable, which means that null
is the only thing that a function returning Void
could return.
Of course public void blah() {...}
(with a lowercase v
) doesn't have to return anything.
If you're wondering about possible uses for Void
, see Uses for the Java Void Reference Type?
Why does the Void class exist?
It is required by the Reflection API, to represent the return type of a void method, as the result of java.lang.reflect.Method.getReturnType().
(Stricly speaking this is void.class,
not Void.class,
but the compiler coerces void.class
to Void.TYPE
.) It is also used in several other places as shown in the Use page in its Javadoc.
Related Topics
Java 'File.Delete()' Is Not Deleting Specified File
Difference Between Running and Starting a Thread
How to Use Mockito When We Cannot Pass a Mock Object to an Instance of a Class
Java - Generate Random Range of Specific Numbers Without Duplication of Those Numbers - How To
Spring MVC + JSON = 406 Not Acceptable
How to Set Working Directory with Processbuilder
Sending the Same But Modifed Object Over Objectoutputstream
Chromedriver and Webdriver for Selenium Through Testng Results in 4 Errors
Java.Lang.Illegalstateexception: Not on Fx Application Thread; Currentthread = Thread-4
Dbpedia Jena Query Returning Null
Double Calculation Producing Odd Result
How to Parse Month Full Form String Using Dateformat in Java
When Does the JPA Set a @Generatedvalue @Id
How to Output Two Columns to the Console in Java
Why Does the "Protected" Modifier in Java Allow Access to Other Classes in Same Package
How to Read a File from a Jar File
How to Parse Case-Insensitive Strings with Jsr310 Datetimeformatter