Using the @SafeVarargs annotation

In Java 9, we can 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 usage is:

    @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:

  • Static methods
  • Final instance methods
  • Private instance methods

Get Java 9: Building Robust Modular Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.