2

When using a wildcards some methods (e.g. ArrayList.set) cannot be used and return an error message similar to

The method set(int, capture#3-of ?) in the type List is not applicable for the arguments (int, String)

I have read why this is the case and would like to know how this is done. That is how does the compiler know that some methods are applicable (e.g. List.get(int)) but others are not?

Community
  • 1
  • 1
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137

2 Answers2

3

All applicable rules are in the Java Language Specification #15.12.

assylias
  • 321,522
  • 82
  • 660
  • 783
2

Compiler assgins special tokens (capture#3-of ?) to each occurence of wildcard and use regular method applicability rules (JLS 15.12.2).

Since captures of wildcards are not compatilbe with other types and with other captures, "method not applicable" is produced.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • So essentially all methods that have a generic parameters are not allowed? – Micha Wiedenmann Jan 08 '13 at 08:36
  • Generic methods/methods of generic classes parameterized by wildcards are not allowed. It's clear from the explanation of wildcards you referred to - wildcard means "unknown type", therefore we cannot pass anything as a parameter of that type. – axtavt Jan 08 '13 at 08:40
  • But a return type parameterized by wildcards is allowed, correct? – Micha Wiedenmann Jan 08 '13 at 08:51