Java annotations required field

Java Guides

In this article, we’ll talk about a set of predefined annotation types in the Java SE API. Some annotation types are used by the Java compiler, and some apply to other annotations.

What is Annotation?

Java has had annotations ever since the 1.5 release. Since then, they’ve shaped the way we’ve designed our applications.

Spring and Hibernate are great examples of frameworks that rely heavily on annotations to enable various design techniques.

Basically, an annotation assigns extra metadata to the source code it’s bound to. By adding an annotation to a method, interface, class, or field, we can:

  • Inform the compiler about warnings and errors
  • Manipulate source code at compilation time
  • Modify or examine behavior at runtime

Java Built-in Annotations from java.lang Package

  1. @Deprecated — A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists.
  2. @Override — Indicates that a method declaration is intended to override a method declaration in a supertype.
  3. @FunctionalInterface — An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification.
  4. @SafeVarargs — A programmer assertion that the body of the annotated method or constructor does not perform potentially unsafe operations on its varargs parameter.
  5. @SuppressWarnings — Indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element).

@Deprecated

The @Deprecated annotation indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with the @Deprecated annotation. When an element is deprecated, it should also be documented using the Javadoc @deprecated tag.

Example: The use of the at sign (@) in both Javadoc comments and in annotations is not coincidental: they are related conceptually. Also, note that the Javadoc tag starts with a lowercase d and the annotation starts with an uppercase D.

// Javadoc comment follows /** * @deprecated * explanation of why it was deprecated */ @Deprecated static void deprecatedMethod() < >>

@Override

@Override annotation informs the compiler that the element is meant to override an element declared in a superclass.

package net.javaguides.annotations; public class OverrideAnnotationExample < public static void main(String[] args) < BaseClass baseClass = new SubClass(); baseClass.test(); SubClass subClass = new SubClass(); subClass.test(); > > class BaseClass < public void test() < System.out.println("Inside baseclass"); > > class SubClass extends BaseClass < @Override public void test() < System.out.println("Inside subclass"); > >
Inside subclass Inside subclass 

While it is not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.

@FunctionalInterface

The @FunctionalInterface annotation indicates that an interface is a functional interface and contains exactly one abstract method.

Читайте также:  Krepcom ru auth index php

Example: Let’s demonstrates the usage of @FunctionalInterface annotation with examples. Let’s create Sayable interface annotated with @FunctionalInterface annotation.

@FunctionalInterface interface Sayable< void say(String msg); // abstract method >
public class FunctionalInterfacesExample < public static void main(String[] args) < Sayable sayable = (msg) - > < System.out.println(msg); >; sayable.say("Say something .."); > >

@SafeVarargs

Java 7 introduced the @SafeVarargs annotation to suppress the unsafe operation warnings that arises when a method is having varargs (variable number of arguments).

package net.javaguides.annotations; import java.util.ArrayList; import java.util.List; public class SafeVarAnnotationExample < @SafeVarargs private void print(List. names) < for (List  String > name: names) < System.out.println(name); > > public static void main(String[] args) < SafeVarAnnotationExample obj = new SafeVarAnnotationExample(); List  String > list = new ArrayList < String > (); list.add("Ramesh"); list.add("John"); obj.print(list); > >

@SuppressWarnings

@SuppressWarnings annotation indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element).

@SuppressWarnings("unchecked") void uncheckedGenerics() < List words = new ArrayList(); words.add("hello"); // this causes unchecked warning >

Annotations That Apply to Other Annotations

Annotations that apply to other annotations are called meta-annotations. There are several meta-annotation types defined in java.lang.annotation.

@Retention

  • RetentionPolicy.SOURCE – The marked annotation is retained only in the source level and is ignored by the compiler.
  • RetentionPolicy.CLASS – The marked annotation is retained by the compiler at compile time, but is ignored by the Java Virtual Machine (JVM).
  • RetentionPolicy.RUNTIME – The marked annotation is retained by the JVM so it can be used by the runtime environment.

@Documented

The @Documented annotation indicates that whenever the specified annotation is used those elements should be documented using the Javadoc tool. (By default, annotations are not included in Javadoc.) For more information, see the Javadoc tools page.

@Target

@Target annotation marks another annotation to restrict what kind of Java elements the annotation can be applied to. A target annotation specifies one of the following element types as its value:

  • ElementType.ANNOTATION_TYPE can be applied to an annotation type.
  • ElementType.CONSTRUCTOR can be applied to a constructor.
  • ElementType.FIELD can be applied to a field or property.
  • ElementType.LOCAL_VARIABLE can be applied to a local variable.
  • ElementType.METHOD can be applied to a method-level annotation.
  • ElementType.PACKAGE can be applied to a package declaration.
  • ElementType.PARAMETER can be applied to the parameters of a method.
  • ElementType.TYPE can be applied to any element of a class.

@Inherited

The @Inherited annotation indicates that the annotation type can be inherited from the superclass. (This is not true by default.) When the user queries the annotation type and the class has no annotation for this type, the class’ superclass is queried for the annotation type. This annotation applies only to class declarations.

@Repeatable

@Repeatable annotation, introduced in Java SE 8, indicates that the marked annotation can be applied more than once to the same declaration or type use.

Reference

Источник

Spring @Required Annotation

The @Required annotation applies to bean property setter methods and it indicates that the affected bean property must be populated in XML configuration file at configuration time. Otherwise, the container throws a BeanInitializationException exception. Following is an example to show the use of @Required annotation.

Example

Let us have a working Eclipse IDE in place and take the following steps to create a Spring application −

Steps Description
1 Create a project with a name SpringExample and create a package com.tutorialspoint under the src folder in the created project.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Create Java classes Student and MainApp under the com.tutorialspoint package.
4 Create Beans configuration file Beans.xml under the src folder.
5 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

Here is the content of Student.java file −

package com.tutorialspoint; import org.springframework.beans.factory.annotation.Required; public class Student < private Integer age; private String name; @Required public void setAge(Integer age) < this.age = age; >public Integer getAge() < return age; >@Required public void setName(String name) < this.name = name; >public String getName() < return name; >>

Following is the content of the MainApp.java file −

package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp < public static void main(String[] args) < ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Student student = (Student) context.getBean("student"); System.out.println("Name : " + student.getName() ); System.out.println("Age : " + student.getAge() ); >>

Following is the content of the configuration file Beans.xml

Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will raise BeanInitializationException exception and print the following error along with other log messages −

Property 'age' is required for bean 'student'

Next, you can try the above example after removing the comment from ‘age’ property as follows −

The above example will produce the following result −

Источник

Оцените статью