5

In my code I (accidentially) wrote void.class (not Void.class) which was happily accepted by the compiler. Until now I thought that the primites are no real objects (not only void, also talking about int, etc... here), so they'd have no class.

I'm talking about all the "primitive classes". Every primitive type has a class denoted by <primitivetype>.class, for example float.class

  • What "Class" is referred to by such a "primitive class", e.g. void.class?
  • what are the possible use cases of these "primitive classes"?
    • Especially, do they offer a constructor?
    • Can we make meaningful instance checks like int.class.isInstance(2.3f)?
WorldSEnder
  • 4,875
  • 2
  • 28
  • 64
  • Possible duplicate of [What is the need of Void class in Java](http://stackoverflow.com/questions/2352447/what-is-the-need-of-void-class-in-java) – David Titarenco Oct 11 '15 at 19:11
  • The javadoc of isInstance says: *If this Class object represents a primitive type, this method returns false.* – JB Nizet Oct 11 '15 at 19:12
  • 1
    @DavidTitarenco That question does not address `void.class`, or the other primitive classes e.g. `int.class` (the top answer - including comments - kind-of does) – WorldSEnder Oct 11 '15 at 19:16
  • @WorldSEnder: gotcha, thanks for the clarification! – David Titarenco Oct 11 '15 at 19:21

3 Answers3

6

void.class is the object used in reflection to indicate that a method has void return type. If you write method.getReturnType();, this object may be returned.

int.class can represent both arguments and return types.

You can write int.class.isInstance(...) but it will always return false, because the argument is an Object.

No Class object has an accessible constructor.

Both int.class.getConstructors() and int.class.getDeclaredConstructors() return an empty array. There are no constructors for primitive types.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
2

The first obvious problem of Class is that it also represents interfaces :)

Prior to java5, Class is used to represent all java types, including primitive types. This is out of convenience, and it worked fine.

After generics was introduced, java types get a lot richer. java.lang.reflect.Type was introduced to include all types. Unfortunately, it incorporates the old messy Class, making an even bigger mess. The hierarchy of Type just doesn't make sense.

Now, void is not even a type! So Class also include a non-type. That is out of convenience, as other answers have shown. Similarly, a wildcard is not a type either, yet WildcardType extends Type!


Could void have been designed as a type in the beginning? Why not. It's a primitive type of 0 bits, having exactly one value (aka, a unit type). I'm not sure how to feel if people write

void foo(void param){
    param = bar();  // void bar(){...}

but at least I'll be happy if I could write

void foo(...){
    return bar();

see also John Rose's comment

ZhongYu
  • 19,446
  • 5
  • 33
  • 61
  • a cleaner representation of java type if anyone's interested - http://zhong-j-yu.github.io/od/1.0.0/javadoc/bayou/jtype/JavaType.html – ZhongYu Oct 11 '15 at 21:15
0

Void is a placeholder for return type void used in Reflections.

Example:- void.class is the return type for the methods returning void as in main method below:-

import java.lang.reflect.Method;

public class A{
    public static void main(String[] args) throws NoSuchMethodException, SecurityException{

    Method mh = A.class.getMethod("main",String[].class);
    System.out.println(mh.getReturnType() == void.class);

    }
}
Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35