What do I return if the return type of a method is Void? (Not void!)

前端 未结 6 1100
忘掉有多难
忘掉有多难 2020-12-07 12:12

Due to the use of Generics in Java I ended up in having to implement a function having Void as return type:

public Void doSomething() {
    //..         


        
6条回答
  •  遥遥无期
    2020-12-07 12:34

    To make clear why the other suggestions you gave don't work:

    Void.class and Void.TYPE point to the same object and are of type Class, not of Void.

    That is why you can't return those values. new Void() would be of type Void but that constructor doesn't exist. In fact, Void has no public constructors and so cannot be instantiated: You can never have any object of type Void except for the polymorphic null.

    Hope this helps! :-)

提交回复
热议问题