SafeVarargs Annotation in Java 9

Posted By : Dinesh Kumar | 25-Sep-2018
Java 9 @SafeVarargs Annotation 
 
Java 9 Safe Varargs Annotation. 
 
It is an annotation which applies on a technique or constructor that takes varargs parameters. It is utilized to guarantee that the strategy does not perform unsafe tasks on its varargs parameters. 
 
It was incorporated into Java7 and must be connected on: 
 
1. Final methods 
2. Static methods
3. Constructors 
 
From Java 9, it can likewise be utilized with : 
 
4. Private instance methods 
 
Note: The @SafeVarargs annotation can be connected just to methods that can't be superseded. Applying to alternate methods will toss a compile time mistake. 
 
How about we see a few precedents, in first model, we are not utilizing @SafeVarargs annotation and incorporating code. It couldn't be any more obvious, what occurs? 
 
Java 9 @SafeVarargs Annotation e.g: 
 
 

import java.util.ArrayList;  

import java.util.List;  

public class SafeVarDemo{  

    private void display(List<String>... objects) { // Not using @SaveVarargs  

        for (List<String> object : objects) {  

            System.out.println(object);  

        }  

    }  

    public static void main(String[] argus) {  

        SafeVarDemo safeVarDemo   = new SafeVarDemo();  

        List<String> objectList = new ArrayList<String>();  

        objectList.add("Bag");  

        objectList.add("Mobile");  

        safeVarDemo.display(objectList);  

    }     

}  

 
 
It produces cautioning messages at compile time, yet compiles without blunders. 
 
Output: 
 
At compile time: 
 
Note: SafeVarDemo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
 
At runtime:
 
[Bag, Mobile] 
 
This is a compiler produced cautioning in regards to unsafe varargs compose. 
 
To evade it, we should utilize @SaveVarargs documentation to the strategy, as we did in the accompanying model. 
 
Java 9 @SafeVarargs Annotation Example 
import java.util.ArrayList;  

import java.util.List;  

public class SafeVarDemo{  

    // Applying @SaveVarargs annotation  

    @SafeVarargs  

    private void display(List<String>... objects) { // Not using @SaveVarargs  

        for (List<String> object : objects) {  

            System.out.println(object );  

        }  

    }  

    public static void main(String[] args) {  

        SafeVarDemo safeVarDemo  = new SafeVarDemo();  

        List<String> objectList = new ArrayList<String>();  

        objectList.add("Bag");  

        objectList.add("Mobile");  

        safeVarDemo.display(objectList);  

    }     

}  

 
 
Presently, compiler does not deliver cautioning message, code compiles and runs effectively. 
 
Output: 
 
[Bag, Mobile] 
 
Note: To apply @SaveVarargs annotation on private instance methods, compile code utilizing Java 9 or higher forms as it were. 
 
What occurs? On the off chance that we compile the accompanying code by utilizing more established variants of Java.
 
 
Java @SafeVarargs Annotation Example
import java.util.ArrayList;  

import java.util.List;  

public class SafeVarDemo{     

    @SafeVarargs  

    private void display(List<String>... obejcts) {  

        for (List<String> obejct : obejcts) {  

            System.out.println(obejct);  

        }  

    }  

    public static void main(String[] args) {  

        SafeVarDemo safeVarDemo = new SafeVarDemo();  

        List<String> list = new ArrayList<String>();  

        list.add("Bag");  

        list.add("Mobile");  

        safeVarDemo.display(list);  

    }     

}  

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

About Author

Author Image
Dinesh Kumar

Dinesh Kumar is an experienced with knowledge of Spring Framework, Spring Boot, Java, Javascript, JQuery, AngularJs, and SQL.

Request for Proposal

Name is required

Comment is required

Sending message..