John Assymptoth
John Assymptoth

Reputation: 8507

In Java, is it possible to cast to void (not Void)?

Is there anything I can put in X, to make the follow work:

Object o = (void) X;

Upvotes: 4

Views: 8064

Answers (4)

dabhaid
dabhaid

Reputation: 3879

Maybe this discussion might be of interest: Uses for the Java Void Reference Type?, it covers about everything you can do with Void and what it's good for.

Upvotes: 3

DwB
DwB

Reputation: 38300

Java is not C++. In Java, void is not a type, it is a placeholder that means "no return value".

Upvotes: 2

Brian
Brian

Reputation: 6450

No. You can set x = null; if you wish though?

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533530

void is notionally a primitive. (though most would disagree it is even that I suspect) You cannot cast an object to it.

The closest you can come to this is an InvocationHandler can return null for a void method and a void method invoke()ed via reflection will return null.

Upvotes: 9

Related Questions