Home  >  Article  >  Java  >  @SafeVarargs annotation for private methods in Java 9?

@SafeVarargs annotation for private methods in Java 9?

王林
王林forward
2023-09-06 22:13:02831browse

Java 9中私有方法的@SafeVarargs注解?

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

Syntax

<strong>@SafeVarargs
private void methodName(...) {
   // some statements
}</strong>

Example

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);
   }
}

Output

<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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete