Java 9 – @SafeVarargs Annotation

In this post, we will see about @SafeVarargs Annotation in Java 9.

The @SafeVarargs is an annotation that is used to perform safe operations. When a method takes variable arguments, then it may cause to unsafe operation, so the @SafeVarargs annotation tells to the compiler to perform safe operations. For example, if we don’t use the annotation the compiler reports warning: Type safety: Potential heap pollution via varargs parameter.

We can use this annotation to final and static and private (from Java 9) methods only of a class. See the example below.

@SafeVarargs annotation

Let’s see few examples with final and static methods.

@SafeVarargs with Final Method

In this example, we have a final methods that takes var-args parameters so we used @SafeVarargs annotation. See the example.


[Python, Java]

@SafeVarargs with Static Method

Java allows using @SafeVarargs annotation with static methods as well. See the example with below.


[Python, Java]

Java 9 @SafeVarargs Improvement

In Java 9, It is allowed to use @SafeVarargs annotation with private methods as well. Here, we have a private method displayString() that takes string type var-args.


[Python, Java] Java

Please note that if you use @SafeVarargs in private method before Java 9, you will get a compilation error.

Without @SafeVarargs annotation

In case, we don’t use the annotation the compile will report a warning message to console with output.


Note: uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.


That’s all about Java 9 @SafeVarargs Annotation.

Was this post helpful?

Related Posts

  • 12 October

    Java 9 JShell tutorial

    Table of ContentsHello World Example Using JShellWhat can we do with JShell?How to start and stop JShell?Creating variable in JShellJShell as a CalculatorCreating Methods in JShellCreating Class in JShellExecuting Algebraic Expressions in JShellExecuting Conditional Statements in JShellJShell CommandsList the VariablesList the MethodsImport Java PackagesCheck history of commandsEdit code in JShell Edit PadLoad external Java code […]

  • 11 October

    Java 9: Stream API Improvements

    Table of ContentsThe takeWhile() MethodExampleThe dropWhile() MethodExampleThe ofNullable() MethodExampleThe iterate() MethodExample In Java 9, following methods are added to Stream interface of stream package. These methods are default and static. The takeWhile() Method This method is included in Stream interface and used to collect the elements into a stream. It takes all the elements till […]

  • 24 September

    Java 9 – Process API Improvements

    Table of ContentsThe Process ClassStart a new Process?MethodsExample to get Process IdExample to get Process InformationProcessHandle InterfaceMethodsExampleProcessHandle.Info Interface Methods In this post, we will see about Java 9 process API improvements. Java improved its Process API in Java 9 version that includes new methods for Process class and two new interfaces ProcessHandle and ProcessHandle.Info. These […]

  • 19 September

    Java try with resources

    Table of ContentsOlder approach to close the resourcesJava 7 try with resourcesSyntaxExampleJava 9 Try with resources ImprovementsFinally block with try with resourcesCreate Custom AutoCloseable Code In this post, we will see about Java try with resources Statement. Java try with resources is a feature of Java which was added into Java 7. It helps to […]

  • 19 September

    Java 9 – Javadoc Improvement

    Table of ContentsWhat is JavadocSimplified Doclet APISupport for HTML5Improved SearchSupport for Module SystemHow to create HTML Javadoc from Java source file.Run Javadoc Command Java has upgraded its Javadoc for better user interaction and user-experience. It includes a simplified search, HTML generator, Doclet API, etc. What is Javadoc The Javadoc is a tool like javac and […]

  • 19 September

    Java 9 – Anonymous Inner classes and Diamond Operator

    Table of ContentsAnonymous class: Java 9 ImprovementExample: Anonymous class with diamond operator Type inference is a feature of Java that was introduced in Java 7. Now, Java compiler can infer type of a data automatically. For example, if we create an ArrayList then we use the code like: [crayon-63c444871faae496726453/] You can see that Java 7 […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.