Java 9 @SafeVarargs Annotation Enhancement

Last updated on July 8th, 2024

@SafeVarargs annotation was introduced in Java 7. @SafeVarargs annotation is used suppressed compiler warnings related to Heap pollution. Heap pollution has occurred when one variable points to any other type of object then the compiler gives a warning message related to this Heap pollution. Until Java 8 @SafeVarargs annotation is used in the constructor, static methods and final methods. But Java 9 @SafeVarargs annotation is used in the private methods also. This is a small enhancement in Java 9 @SafeVarargs annotation.

Table of content

1. What is Varargs in Java?
2. How to declare the Varargs method in Java?
3. Need of Varargs in Java
4. Java 9 @SafeVarargs annotation example
5. Java 8 @SafeVarargs with private method annotation example
6. Conclusion

1. What is Varargs in Java?

Variable Arguments(Varargs) in Java is a method argument that takes any number of values of that declared type.

2. How to declare the Varargs method in Java?

Syntax of Varargs method in Java:

return_type method_name(data_type? variable_name){
    // method body
}

3. Need of Varargs in Java

Until Java 4 we can’t pass any number of values in already created methods. We need to create a new method with the same number of arguments in the program. This will increase the length of the code of the program and reduce the readability of the code. So Java resolves this problem to introduce the varargs feature in Java 5.  

4. Java 9 @SafeVarargs annotation example

Example without using @SafeVarargs annotation

import java.util.ArrayList;
import java.util.List;
public class Test {
  private void display(List<String>...list) {
    for (List<String> name: list) {
      System.out.println(name);
    }
  }
  public static void main(String[] args) {
    Test test = new Test();
    List<String> listOfData = new ArrayList<String>();
    listOfData.add("Peter");
    listOfData.add("Robert");
    test.display(listOfData);
  }
}


Output

[Peter, Robert]
Note: Test.java uses unchecked or unsafe operations.
Note: Recompile with - Xlint: unchecked
for details.

Example with using @SafeVarargs annotation

import java.util.ArrayList;
import java.util.List;
public class Test {
  @SafeVarargs
  private void display(List<String> ...list) {
    for (List<String> name: list) {
      System.out.println(name);
    }
  }
  public static void main(String[] args) {
    Test test = new Test();
    List<String> listOfData = new ArrayList<String>();
    listOfData.add("Peter");
    listOfData.add("Robert");
    test.display(listOfData);
  }
}

Output

[Peter, Robert]

5. Java 8 @SafeVarargs with private method annotation example

import java.util.ArrayList;
import java.util.List;
public class Test {
  @SafeVarargs
  private void display(List<String> ...list) {
    for (List<String> name: list) {
      System.out.println(name);
    }
  }
  public static void main(String[] args) {
    Test test = new Test();
    List<String> listOfData = new ArrayList<String>();
    listOfData.add("Peter");
    listOfData.add("Robert");
    test.display(listOfData);
  }
}  

Output

Test.java:5: error: Invalid SafeVarargs annotation. Instance method display(List<String>...) is not final.
    private void display(List<String>... names) {  
                 ^
Note: Test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

6. Conclusion

In this topic, we learnt about @SafeVarargs annotation, Java 9 @SafeVarargs annotation enhancement, what is varargs in java, the need for varargs in java and example of @SafeVarags in Java 9 and Java 8.

Leave a Comment