4

I referred to documentation on Java Generics, and trying to use the wildcard "?" in a simple program:

class Unknown <?> {

}

public class UnknownTypes {

    public static void main(String s[] ) {

    }
}

Wildcard "?" refers to an Unknown type, so in class Unknown, I used type-parameter wildcard itself; however when I compile I get compilation error. If I had used like this it would have worked.

class Unknown <T> {

}

If wildcard "?" refers to unknown type, why can't we use "?" as type parameter.

The following is the compile error that I get.

UnknownTypes.java:1: error: <identifier> expected
class Unknown <?> {
           ^
UnknownTypes.java:1: error: '{' expected
class Unknown <?> {
            ^
UnknownTypes.java:10: error: reached end of file while parsing
}

Is wildcard "?" used in conjunction with something else?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
CuriousMind
  • 8,301
  • 22
  • 65
  • 134
  • Unknown in the sense that it will never be known. The wildcard doesn't make sense in a class declaration because when you instantiate the class, you'll parameterize it with a type. It's more useful for things like `List> list = nonGenericGetList();` (you'll do this a lot when working with old libraries). – David Ehrmann Jul 14 '14 at 15:53
  • What are you trying to solve with an unknown type here? – Philip Whitehouse Jul 14 '14 at 15:55
  • To illustrate David's statement, imagine how you will use the parameter had you are allowed to use ?. For instance, a class member declaration such as `private ? a` would be legal, which is neither allowed nor make sense. – Evan Sebastian Jul 14 '14 at 15:58
  • see exactly same question http://stackoverflow.com/questions/9914302/why-wild-cards-cant-be-used-in-generic-class-method-declaration – Vipin Jul 14 '14 at 16:47

4 Answers4

3

You can't name a generic parameter as ?, because ? is not a valid identifier - a valid name of a variable. You have to give a generic parameter a valid java name so you can refer to it in the implementation.

You can only specify ? as a generic bound:

List<?> list; // a variable holding a reference to a list of unknown type

You can't use ? when creating an instance:

new ArrayList<?>(); // can't do this

or as a parameter name of a class :

class MyClass<?> { // can't do this
Bohemian
  • 412,405
  • 93
  • 575
  • 722
3

To define a generic class with a type parameter, you cannot use a wildcard (in the generic class it is a type)

class Unknown <TYPE> {
  TYPE foo; // <-- specifies the type of foo.
}

When using it, you can use the wildcard

Unknown<?> some = new Unknown<String>(); // <-- some is any kind of Unknown.
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

You only use the wildcard when referring to an instance of a class. Not in the class declaration.

class Unknown<T>{}

Unknown<?> instance= new Unknown<Integer>();


public void canHandleAnyUnknown(Unknown<?> wild){
}
John B
  • 32,493
  • 6
  • 77
  • 98
0

Wildcard means 'anything' not unknown. You can use only Alphabets as type parameter.

whoami
  • 1,517
  • 1
  • 21
  • 29