I am not clear with the class java.lang.Void
in Java. Can anybody elaborate in this with an example.
![](../../users/profiles/34509.webp)
- 466,055
- 116
- 851
- 1,175
![](../../users/profiles/240698.webp)
- 24,958
- 61
- 134
- 175
4 Answers
It also contains Void.TYPE
, useful for testing return type with reflection:
public void foo() {}
...
if (getClass().getMethod("foo").getReturnType() == Void.TYPE) ...
![](../../users/profiles/103154.webp)
- 228,184
- 37
- 489
- 472
-
+1 for pointing that it is used in reflection to specify the type of a method, which has no return value. – stacker Feb 28 '10 at 21:09
-
1Actually there is also void.class (lowercase!) which would also works perfectly in your example. So Void is not needed there. – whiskeysierra Feb 28 '10 at 23:27
-
8In fact, Void.TYPE is defined as public static final Class
TYPE = Class.getPrimitiveClass("void"); Which itself is void.class. – whiskeysierra Feb 28 '10 at 23:29 -
@Willi: Yes, I forget about `void.class`. Sorry. – axtavt Mar 01 '10 at 00:44
-
1Did `void.class` exist in Java 1.1? – Cephalopod Aug 15 '11 at 11:52
-
@Cephalopod yes, check out javadoc param @since for Void class it says: `@since JDK1.1` – sactiw Jun 15 '15 at 16:50
Say you want to have a generic that returns void for something:
abstract class Foo<T>
{
abstract T bar();
}
class Bar
extends Foo<Void>
{
Void bar()
{
return (null);
}
}
![](../../users/profiles/65868.webp)
- 58,140
- 15
- 111
- 160
-
13
-
3I like TofuBeer's answer, but Roman raised an interesting point there. – whiskeysierra Feb 28 '10 at 23:30
-
1It isn't there strictly for that, it was just the first use that jumped to mind. The reflection answer is why it was introduced. – TofuBeer Mar 01 '10 at 00:39
-
4Had this use case and was stumped when javac wrote `This method must return a result of type Void`. Just returning `(null)` didn't come to mind. – oschrenk May 21 '12 at 15:26
Actually there is a pragmatic case where void.class is really useful. Suppose you need to create an annotation for class fields, and you need to define the class of the field to get some information about it (in example, if the field is an enum, to get list of potential values). In that case, you would need something like this:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PropertyResourceMapper
{
public Class acceptedValues() default void.class;
}
to be used like this:
@PropertyResourceMapper(acceptedValues = ImageFormat.class, description = "The format of the image (en example, jpg).")
private ImageFormat format;
I have used this to create a custom serializer of classes to a proprietary format.
![](../../users/profiles/825342.webp)
- 185
- 10
From the Java docs:
public final class Void
extends Object
The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.
static Class<Void> TYPE
The Class object representing the primitive Java type void.
TYPE
public static final Class<Void> TYPE
The Class object representing the primitive Java type void.
![](../../users/profiles/760746.webp)
- 8,881
- 2
- 42
- 86
![](../../users/profiles/420631.webp)
- 59
- 1
-
9
-
1@Hugo That is true, but I still found this to be the most useful answer. Other answers sidestep why it exists. It is the Class representation of a primitive type. Like Integer or Double. – scottbot95 Oct 22 '13 at 07:29
-
1You should also mention that the only term that inhabits Void is null. – Prof Mo Feb 22 '14 at 16:11