Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does getClass().getName() return void?

Tags:

java

class

The java documentation for Class.getName() says:

Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.

When will it return void?

like image 200
Kumar Abhinav Avatar asked Jan 09 '14 18:01

Kumar Abhinav


People also ask

What does Getclass () getName () Do Java?

The getName() method of java Class class is used to get the name of the entity, and that entity can be class, interface, array, enum, method, etc. of the class object.

What does getName () do in Java?

getName() returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.


1 Answers

It will give you void String for class literal for void type:

Class<Void> clazz = void.class;
System.out.println(clazz.getName());

Refer JLS § 15.8.2 for further reading:

A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type void, followed by a '.' and the token class.
[...]
The type of void.class (§8.4.5) is Class<Void>.

like image 129
Rohit Jain Avatar answered Oct 14 '22 03:10

Rohit Jain