Skip to main content

Uses for the Java Void Reference Type?



There is a Java Void -- uppercase V-- reference type . The only situation I have ever seen it used is to parameterize Callable s







final Callable<Void> callable = new Callable<Void>() {

public Void call() {

foobar();

return null;

}

};







Are there any other uses for the Java Void reference type? Can it ever be assigned anything other than null ? If yes, do you have examples?


Comments

  1. Void has become convention for a generic argument that you are not interested in. There is no reason why you should use any other non-instantiable type, such as System.

    It is also often used in for example Map values (although Collections.newSetFromMap uses Boolean as maps don't have to accept null values) and java.security.PrivilegedAction.

    I wrote a weblog entry on Void a few years back.

    ReplyDelete
  2. Given that there are no public constructors, I would say it can't be assigned anything other than null. I've only used it as a placeholder for "I don't need to use this generic parameter," as your example shows.

    It could also be used in reflection, from what its Javadoc says:


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

    ReplyDelete
  3. Future<Void> works like charm. :)

    ReplyDelete
  4. You can create instance of Void using reflections, but they are not useful for anything. Void is a way to indicate a generic method returns nothing.

    ReplyDelete
  5. All the primitive wrapper classes (Integer, Byte, Boolean, Double, etc.) contain a reference to the corresponding primitive class in a static TYPE field, for example:

    Integer.TYPE == int.class
    Byte.TYPE == byte.class
    Boolean.TYPE == boolean.class
    Double.TYPE == double.class


    Void was initially created as somewhere to put a reference to the void type:

    Void.TYPE == void.class


    However, you don't really gain anything by using Void.TYPE. When you use void.class it's much clearer that you're doing something with the void type.

    As an aside, the last time I tried it, BeanShell didn't recognise void.class, so you have to use Void.TYPE there.

    ReplyDelete
  6. Before generics, it was created for the reflection API, to hold TYPE returned by Method.getReturnType() for a void method, corresponding to the other primitive type classes.

    EDIT: From the JavaDoc of Void: "The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void". Prior to Generics, I am aware of no use other than reflection.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex