- Mastering Java 11
- Dr. Edward Lavieri
- 108字
- 2021-08-13 15:43:24
Using the @SafeVarargs annotation
Starting with Java 9, we could use the @SafeVarargs annotation with private instance methods. When we use this annotation, we are asserting that the method does not contain any harmful operations on the varargs passed as parameters to the method.
The syntax for use is as follows:
@SafeVarargs // this is the annotation
static void methodName(...) {
/*
The contents of the method or constructor must not
perform any unsafe or potentially unsafe operations
on the varargs parameter or parameters.
*/
}
Use of the @SafeVarargs annotation is restricted to the following:
- Static methods
- Final instance methods
- Private instance methods