Java 9 – @SafeVarargs Annotation (with examples)

Java 7 introduced the @SafeVarargs annotation to suppress the unsafe operation warnings that arises when a method is having varargs (variable number of arguments). The @SafeVarargs annotation can only be used with methods (final or static methods or constructors) that cannot be overriden because an overriding method can still perform unsafe operation on their varargs (variable number of arguments).

Java 9 extended the use of @SafeVarargs annotation, it can now used with private methods as well. This is because private methods cannot be overridden. Earlier this annotation was limited to the final or static methods, or constructors but now it can be used with private methods.

Note: If you try to compile the above code in Java 7 and Java 8, you will get a compilation error because this enhancements is done in Java 9, prior to java 9 – private methods are not allowed to be marked with this annotation.