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

@SafeVarargs annotation for private methods in Java 9?

王林
Release: 2023-09-06 22:13:02
forward
912 people have browsed it

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>
Copy after login

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);
   }
}
Copy after login

Output

<strong>[TutorialsPoint, Tutorix]</strong>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template