...
This noncompliant code example declares a vararg method using Object
. It accepts an arbitrary mix of parameters of any object type. Although Legitimate uses of such declarations have legitimate uses are rare (see exception below), those uses rarely arise.
Code Block | ||
---|---|---|
| ||
ReturnType function(Object... args) { } |
...
This noncompliant code example declares a vararg method using a generic type parameter. It accepts a variable number of parameters that are all of the same object type. Again, although legitimate uses of such declarations have legitimate uses, those uses rarely ariseare rare.
Code Block | ||
---|---|---|
| ||
<T> ReturnType function(T... args) { } |
...
Wiki Markup |
---|
Retrofitting old methods containing final array parameters with generically-typed varargs is not always a good idea. For example, given a method that does not accept an argument of a particular type, it could be possible to override the compile-time checking --- through the use of generic varargs parameters --- so that the method would compile cleanly rather than correctly causing a compile-time error.\[[Bloch 2008|AA. Bibliography#Bloch 08]\]. |
...
DCL09-EX1: Varargs signatures using Object
and imprecise generic types are only acceptable when the body of the method both uses no casts or auto-boxing, and also compiles without error. Consider the following example, which operates correctly for all object types and type-checks successfully.
...