Java Standard Annotations

Java Standard Annotations

Summary: At the end of this tutorial, you will be practicing predefined Java Standard Annotations. Explained in simple terms and example code understandable to a Beginner. Start reading.

After knowing what are annotations in Annotations Introduction,
let us see the built-in annotations that comes along with the JDK (from JDK 1.5) installation.

Java Standard Annotations

There are two types of built-in annotations – Simple annotations and Meta annotations.

Simple Annotations

Simple annotations are compiler instructions (complier shows warnings). There are three simple annotations.

1. @Override: Checks the method is there or not in one of the super classes. If you annotate a method display() in subclass with @override, the compiler checks the same method display() exists in any one of the super classes. That is, compiler checks, the subclass method is an overridden method or not.

2. @Deprecated: If a method is found problematic after the release of the software product, in the next version, the method can be annotated with @Deprecated. If anyone uses the deprecated method, the compiler raises a warning. For example, the readLine() method of DataInputStream of JDK 1.0 version is deprecated from JDK 1.2 onwards; means, not advised to use. The programmer can treat a deprecated method as an obsolete method (which may be discarded in future editions).

3. @SuppressWarnings: Tells the compiler not to show any warnings during compilation like deprecation, unchecked and unsafe operation etc.

Let us explain the three Java Standard Annotations programmatically.

1. Using @Override

In the following code, @override is used.

class Test
{
  public void display() {  System.out.println("Hello 1");  }
}
public class Demo extends Test
{
  @Override
  public void dasplay() {  System.out.println("Hello 2");  }

  public static void main(String args[])
  {
    Demo d1 = new Demo();
    d1.display();
  }
}   

image

@Override is known as annotation. Annotation starts with @ symbol. Now what the annotation @Override has done?

     @Override
     public void dasplay() {  System.out.println("Hello 2");  }

The compiler checks a method by name dasplay() exists in the super class Test or not. As the method dasplay() is not found, the complier gave the above error message.

Here, @Override describes the data (here, data is a method) of how the compiler should behave if the method is not overridden. For this reason, annotations are called metadata (data about data).

Future Java is with annotations only. Already, Hibernate and Spring frameworks are using annotations (I will give a small code later at the end). Annotations make code less, easy and simple to the developer (provided you well understand the purpose of the annotation).

Now who has written the annotation @Override and its meaning to the compiler? It is a predefined annotation built into the language itself from JDK 1.5. Annotations can be applied to packages, parameters, variables, methods and classes. Annotations can be declared along with the source code and writing like this is known as declarative programming.

2. Using @Deprecated

In the class Test, the method display() is annotated (marked) with @Deprecated.

class Test
{
  @Deprecated
  public void display() 
  {  
    System.out.println("Hello 1");  
  }
}
public class Demo
{
  public static void main(String args[])
  {
    Test t1 = new Test();
    t1.display();  
  }
}

image
In the class Test, display() is annotated with @Deprecated. If anyone uses the display() method of Test class, the compiler raises warning. The warning is shown in the above screenshot. display() method is described with @Deprecated and for this reason annotations are known as metadata.

If the annotation @Deprecated is placed before a class, the whole class methods, constructors and variables are deprecated. Observe.

     @Deprecated
     class Test {   }

Now the whole class (its constructors, methodse) is deprecated.

3. @SuppressWarnings

Many constructors and methods were deprecated in Java API. For example, many methods of Date class were deprecated (in favor of Calendar class). Similarly, when you are using a non-generics data structure, the compiler raises an "unchecked and unsafe operation". These warnings, you are well aware of and still does not like them to see at compilation stage, use the @SuppressWarnings annotation.

Observe the following code and its screenshot.

import java.util.*;
public class Demo
{
  public static void show()
  {
    Date today = new Date();
    int month = today.getMonth();
  }
  public static void display()
  {
    List studentNames = new ArrayList();
    studentNames.add("hello");
  }
}

Java Standard Annotations

Date class method getMonth() is deprecated and List object studentNames is non-generics. Both are used in the above code. The compiler displays the warnings as in the above screen.

Now you would like not to see them while compilation. Just add @SuppressWarnings to the method where you are using these.

import java.util.*;
public class Demo
{
  @SuppressWarnings(value={"deprecation"})      // to suppress deprecation warning
  public static void show()
  {
    Date today = new Date();
    int month = today.getMonth();
  }

  @SuppressWarnings(value={"unchecked"})   // to suppress unchecked warning
  public static void display()
  {
    List studentNames = new ArrayList();
    studentNames.add("hello");
  }
}

image

Now you see, in the above screenshot, no warnings are displayed.

The following statements

     @SuppressWarnings(value={"unchecked"})
     public static void display()

can be written as follows also

     @SuppressWarnings({"unchecked"})
     public static void display()

The possible values for @SuppressWarnings are all, deprecation, fallthrough, finally, path, serial and unchecked. One of these values must be specified with the suppress annotation.

To see all the supported values of @SuppressedWarnings on your JDK, just enter X with javac command, as javac -X. Observe the screenshot how I have given.

Java Standard Annotations
Actually annotations are not part of programming logic. Annotations permit developers to include (just to declare) additional information (called metadata) into the source code. Annotations do not change the execution of the program but much used by many tools. The strength of annotations (where your code simplified) can be seen in frameworks like Axis, Hibernate and Spring. Annotations avoid human mistakes in coding and due automated processing decreases code development time.

Annotations can be used to pass special instructions to Java compiler, Java runtime and Java build-time tools.

With this tutorial, now I am sure, you know pretty well the meaning and purpose of annotations. Now we have seen the three simple annotations – @Override, @Deprecated and @SuppressWarnings.

In JDK 1.8, Java added one more standard annotation – @FunctionalInterface. For @FunctionalInterface, refer JDK 1.8 Features.

Next let us read Meta annotations

Pass your comments about the understandability and improve quality of this tutorial Java Standard Annotations.

2 thoughts on “Java Standard Annotations”

  1. “These warnings, you are aware of well and still does not like them to see at compilation stage”
    Grammatical mistakes in above content under @SuppressWarnings(first para).
    It should have been in the following way:
    “These warnings, you are well aware of and still do not like them to see at compilation stage”

Leave a Comment

Your email address will not be published.