8

Possible Duplicate:
What is the difference between java.lang.Void and void?

I wanted to create a concrete class for SwingWorker with both final and intermediate result types as void. I wrote the following code:

class AnswerWorker extends SwingWorker<void, void> {
    protected void doInBackGround() {
        System.out.println("what is your problem!!");
    }
}

This gave me the following error:

Multiple markers at this line-
    Syntax error on token "void", Dimensions expected after this token.
    Syntax error on token "void", Dimensions expected after this token.

However when i changed the code from void to Void (i.e. small v to capital V), it worked fine, although I was still forced to return null; at the end of doInBackground() method.

Why is this? I know Void is a class in java.lang package but the documentation doesn't says much about it (atleast not which i could follow :p)

Thanx in advance!!

Community
  • 1
  • 1
Surender Thakran
  • 3,958
  • 11
  • 47
  • 81

2 Answers2

13

class AnswerWorker extends SwingWorker<Void, Void>
Should do the trick. That is because generics always need objects as parameters. They also can't take primitives. Void is like a Wrapper to get rid of this issue.

Now the cause why you still need to return something:
As the documentation says, Void is still a class. So you need still to return an object. Since it's uninstantiable, there's only the possibility of returning null. So Void isn't fully like the void keyword. There's still the need to return a reference to an object (null-reference in this case).

There's one important thing: Generics force you to use objects. void, like primitive types, isn`t an object. That's the pure oop nature of Java ;)

Zhedar
  • 3,480
  • 1
  • 21
  • 44
3

The java.lang.Void class was originally introduced long before generics as a place to put the constant Void.TYPE, the Class object that represents the void type, and which is returned by java.lang.reflect.Method.getReturnType() for void methods (by analogy to Integer.TYPE, Character.TYPE, etc. which represent int, char, ...).

The Void class can't be instantiated (therefore the only valid value you can assign to a variable of type Void is null) but it has been co-opted since the introduction of generics as a useful token for situations like this where the syntax requires a class name but you don't really want to use one.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • Could you point me to some official/authoritative documentation to confirm this sentence in the context of `SwingWorker`?: _it has been co-opted since the introduction of generics as a useful token for situations like this where the syntax requires a class name but you don't really want to use one_. – Jaime Hablutzel Aug 10 '15 at 17:28
  • @JaimeHablutzel I can't, I chose the term "co-opted" to imply that it's an unofficial convention rather than an official recommendation. If you're always going to return `null` then it doesn't really matter what class you use, but `Void` is a useful convention. The same convention is used by [`java.util.concurrent.RecursiveAction`](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveAction.html), it's a "resultless" `ForkJoinTask` that always returns `null`, so it extends `ForkJoinTask`. – Ian Roberts Aug 10 '15 at 21:36
  • the fact that there is a class in the standard Java API using `Void` like that seems quite official reference to me. Thanks!. – Jaime Hablutzel Aug 11 '15 at 21:51