Java annotations

Dictionary meaning of Annotation

Annotation refers to a note by way of explanation or comment added to a text or diagram.

Java Annotations:

Java annotations are used to provide the meta data to our Java code. Meta data is the additional information which can be used for any class, interface, method or field. Java annotations can be used as an alternative option for XML and java marker interfaces. Java annotations were added to Java from Java 5.

Use of java annotations

Java annotations are mainly used for the following:

  • Compiler instructions
  • Build-time instructions
  • Runtime instructions

Compiler instructions: Java provides the 3 in built annotations which are used to give certain instructions to the compiler. Java in built annotation are @Deprecated, @Override & @SuppressWarnings.

Build-time instructions: Java annotations can be used for build time or compile time instructions. These instructions can be used by the build tools for generating source code, compiling the source, generating XML files, packaging the compiled code and files into a JAR file etc.

Runtime instructions: Normally, Java annotations are not present in your Java code after compilation. However, we can define our own annotations that can be available at runtime. These annotations can be accessed using Java Reflection.

Java annotations basics:

A java annotation always starts with the symbol @ and followed by the annotation name. The @symbol signals the compiler that this is an annotation.

Syntax:

@AnnotationName

Example:

@Entity

Here @ symbol signals the compiler that this is an annotation and the Entity is the name of this annotation.

An annotation can contain zero, one or multiple elements. We have to set values for these elements. Example:

@Entity(tableName = "USERS")

Where we can use annotations?

We can use java annotations above classes, interfaces, methods, fields and local variables. Here is an example annotation added above a class definition:

@Entity
 public class Users {
}

Java Annotations List:

  • @Deprecated
  • @Override
  • @SuppressWarnings
  • @Target
  • @Retention
  • @Inherited
  • @Documented

Built-in Java Annotations:

Java provides three built-in annotations which are used to give certain instructions to the compiler. Built-in java annotations are as follows:

  • @Deprecated Java Annotation.
  • @Override Java Annotation.
  • @SuppressWarnings java annotation.

@Deprecated java annotation:

The deprecated terms in software development is refers to classes, interfaces, functions or elements that are in the process of being replaced by newer ones. The @Deprecated java annotation signals that the marked element is deprecated and should no longer be used. It is better not to use deprecated methods because these methods can be removed from future versions.

Note: We can use @deprecated Javadoc tag in the comment section to inform the developers about the reason of deprecation and what can be used in place of this deprecated method, class or field.

package com.tutorialspointexamples;
 
class DerecatedTestClass{  
	void display(){
		System.out.println("Display Method.");
	}  
 
	/* 
	 * @deprecated replaced by display()
         */
	@Deprecated  
	void show(){
		System.out.println("Show Method is deprecated.");
	}  
}  
 
public class DeprecatedTest {
	public static void main(String args[]){
		DerecatedTestClass obj = new DerecatedTestClass();
		obj.show();
		obj.display();
	}
}

Output:

Show Method is deprecated.
Display Method.

Download this example.

@Override java annotation:

The @Override java annotation indicates that the subclass method is overriding the parent class method. Compile time error occurs if it not overrides the specified method. As override annotation is not mandatory to use while overriding the method so if we are not using override annotation it may be the case someone changed the name of the overridden method in the superclass. In that case subclass method would no longer override it. So we always have to use @Override annotation which signals the compiler that the method in the subclass is not overriding any method in the superclass.

package com.tutorialspointexamples;
 
class ParentClass{  
	void display(){
		System.out.println("Parent Class Display Method.");
	}  
}  
 
public class SubClass extends ParentClass{
	@Override
	void display(){
		System.out.println("Sub Class Display Method.");
	}
	public static void main(String args[]){
		SubClass obj = new SubClass();
		obj.display();
	}
}

Output:

Sub Class Display Method.

Download this example.

@SuppressWarningsjava annotation:

The @SuppressWarnings java annotation is used to instruct the compiler to ignore or suppress warnings.

package com.w3spoint;
 
class DerecatedTestClass{  
	void display(){
		System.out.println("Display Method.");
	}  
 
	/* 
	 * @deprecated replaced by display()
     */
	@Deprecated  
	void show(){
		System.out.println("Show Method is deprecated.");
	}  
}  
 
public class SuppressWarningTest {
	@SuppressWarnings("deprecation")
	public static void main(String args[]){
		DerecatedTestClass obj = new DerecatedTestClass();
		obj.show();
		obj.display();
	}
}

Output:

Show Method is deprecated.
Display Method.

Download this example.

Please follow and like us:
Content Protection by DMCA.com