returning a Void object

前端 未结 5 1271
故里飘歌
故里飘歌 2020-12-09 00:17

What is the correct way to return a Void type, when it isn\'t a primitive? Eg. I currently use null as below.

interface B{ E method();          


        
5条回答
  •  青春惊慌失措
    2020-12-09 01:16

    The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.

    So any of the following would suffice:

    • parameterizing with Object and returning new Object() or null
    • parameterizing with Void and returning null
    • parameterizing with a NullObject of yours

    You can't make this method void, and anything else returns something. Since that something is ignored, you can return anything.

提交回复
热议问题