BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java 9 – @SafeVarargs Annotation (with examples)

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

Java 7 introduced the @SafeVarargs annotation to suppress the unsafe operation warnings that arises when a method is having varargs (variable number of arguments). The @SafeVarargs annotation can only be used with methods (final or static methods or constructors) that cannot be overriden because an overriding method can still perform unsafe operation on their varargs (variable number of arguments).

Java 9 extended the use of @SafeVarargs annotation, it can now used with private methods as well. This is because private methods cannot be overridden. Earlier this annotation was limited to the final or static methods, or constructors but now it can be used with private methods.

Java 9 Example – When we do not use @SafeVarargs annotation?

import java.util.ArrayList;  
import java.util.List;  
public class JavaExample{  
    // We are not using @SafeVarargs annotation - Java 9
    private void print(List... names) {  
        for (List<String> name : names) {  
            System.out.println(name);  
        }  
    }  
    public static void main(String[] args) {  
        JavaExample obj = new JavaExample();  
        List<String> list = new ArrayList<String>();  
        list.add("Kevin");  
        list.add("Rick"); 
        list.add("Negan");
        obj.print(list);  
    }     
}

Warnings:

Type safety: Potential heap pollution via varargs parameter names
Type safety: A generic array of List is created for a varargs 
 parameter

Output:

[Kevin, Rick, Negan]

As you can see the code ran fine but it produced few warnings.

Screenshot of this code in Eclipse Oxygen IDE showing the warnings.
Java 9 @SafeVarargs annotation

Java 9 – @SafeVarargs Annotation Example

Lets run the same code again after using the @SafeVarargs annotation.

import java.util.ArrayList;  
import java.util.List;  
public class JavaExample{  
    @SafeVarargs
    private void print(List... names) {  
        for (List<String> name : names) {  
            System.out.println(name);  
        }  
    }  
    public static void main(String[] args) {  
        JavaExample obj = new JavaExample();  
        List<String> list = new ArrayList<String>();  
        list.add("Kevin");  
        list.add("Rick"); 
        list.add("Negan");
        obj.print(list);  
    }      
}

The same output without any warnings.

Note: If you try to compile the above code in Java 7 and Java 8, you will get a compilation error because this enhancements is done in Java 9, prior to java 9 – private methods are not allowed to be marked with this annotation.

Top Related Articles:

  1. Java Enum Tutorial with examples
  2. Method Overloading “Reference is Ambiguous” error in Java
  3. Ambiguity error while overloading Method with Varargs parameter
  4. Java Varargs explained with examples
  5. What is new Keyword in Java

Tags: Java-9

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap