Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the need of Void class in Java [duplicate]

Tags:

java

void

I am not clear with the class java.lang.Void in Java. Can anybody elaborate in this with an example.

like image 511
giri Avatar asked Feb 28 '10 20:02

giri


People also ask

What does a void class do?

void. class is the object used in reflection to indicate that a method has void return type.

How do you duplicate a class in Java?

Creating a copy using the clone() method The class whose object's copy is to be made must have a public clone method in it or in one of its parent class. Every class that implements clone() should call super. clone() to obtain the cloned object reference. The class must also implement java.

What is void in Java?

The void keyword in Java denotes that a method does not have a return type. However, even though a constructor method can never have a return type, it does not have the void keyword in its declaration.

Is void a wrapper class?

- Unlike the other wrappers Void class doesn't store a value of type void in itself and hence is not a wrapper in true essence. - The Void class according to javadoc exists because of the fact that some time we may need to represent the void keyword as an object.


1 Answers

It also contains Void.TYPE, useful for testing return type with reflection:

public void foo() {} ... if (getClass().getMethod("foo").getReturnType() == Void.TYPE) ... 
like image 144
axtavt Avatar answered Oct 15 '22 18:10

axtavt