@SafeVarargs Annotations were introduced in Java 7. This annotation applies to final and static methods or constructors that take variadic parameters. This annotation is used to ensure that the method does not perform unsafe operations on its variadic parameters. Starting with Java 9, the @SafeVarargs annotation also applies to private instancemethods.
<strong>@SafeVarargs private void methodName(...) { // some statements }</strong>
import java.util.ArrayList; import java.util.List; public class SafevarargsTest { <strong>@SafeVarargs // Apply @SafeVarargs to private methods</strong> private void display(List<String>... names) { for(List<String> name : names) { System.out.println(name); } } public static void main(String args[]) { SafevarargsTest test = new SafevarargsTest(); List<String> list = new ArrayList<String>(); list.add("TutorialsPoint"); list.add("Tutorix"); test.display(list); } }
<strong>[TutorialsPoint, Tutorix]</strong>
The above is the detailed content of @SafeVarargs annotation for private methods in Java 9?. For more information, please follow other related articles on the PHP Chinese website!