Java Annotations

Java Annotations are a special type of metadata that can be added to Java source code. They provide additional information about the code and can be processed at compile-time or runtime by annotation processing tools.

Annotations can be used to specify metadata such as:

  1. Information about the code’s dependencies and requirements
  2. Information about the code’s behavior and relationships
  3. Information about the code’s performance characteristics

Here is a simple example of a car class annotated with the @Entity annotation, which indicates that the class should be treated as a database entity:

In this example, the @Entity annotation is used to specify that the Car class should be treated as a database entity. The annotation processing tool can use this information to generate the necessary SQL statements for creating a database table for the Car class, based on the class’s fields and annotations.

Here is a table showing different types of annotations in Java, their descriptions, and examples using a car class:

Annotation TypeDescriptionExample
@OverrideSpecifies that a method is meant to override a method in a superclass.@Override public void startEngine() {...}
@DeprecatedIndicates that a class, method, or field is outdated and should not be used.@Deprecated public void driveWithManualTransmission() {...}
@SuppressWarningsSuppresses specific compiler warnings for the annotated element.@SuppressWarnings("unchecked") public void setPassengers(List passengers) {...}
@RetentionSpecifies the retention policy for the annotated annotation.@Retention(RetentionPolicy.RUNTIME) public @interface CarAnnotation {...}
@DocumentedIndicates that the annotated element should be documented by javadoc.@Documented public @interface CarProperties {...}

more annotations:

Annotation TypeDescriptionExample
@AutowiredSpecifies that a property or constructor parameter should be injected with a bean from the Spring context.@Autowired private Engine engine;
@ValueSpecifies a default value for a property or method parameter.@Value("4") private int numberOfWheels;
@PostConstructSpecifies a method to be called after bean creation is complete, typically for setup or configuration purposes.@PostConstruct public void init() {...}
@PreDestroySpecifies a method to be called before a bean is destroyed, typically for cleanup purposes.@PreDestroy public void cleanup() {...}
@ControllerSpecifies a class as a Spring MVC controller.@Controller public class CarController {...}

Leave a Comment

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

Scroll to Top