ProgrammingJava Developer

What are annotations in Java, how are they structured and used in programming?

Pass interviews with Hintsage AI assistant

Answer

Annotations were added in Java 5 to provide metadata that can be used by the compiler and various frameworks. Initially, they helped tools (such as JUnit or Hibernate) to better interact with the code without the need to write repetitive or external configuration.

Annotations allow adding information to classes, methods, fields, parameters, and even local variables. They can be used during compilation, runtime, or source code processing for validation, generating additional files, or changing the program's behavior.

To declare your own annotation, the @interface keyword is used.

Example code:

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { String value(); } class Example { @MyAnnotation("Test") public void annotatedMethod() {} }

Key features:

  • Annotations can have parameters (with limited types)
  • Annotations can have different scopes (SOURCE, CLASS, RUNTIME)
  • You can create your own annotations to extend standard functionality

Tricky questions.

Do annotations only affect runtime?

No, annotations can be limited to source code (SOURCE), bytecode (CLASS), or be available at runtime (RUNTIME). Their influence depends on RetentionPolicy.

@Retention(RetentionPolicy.SOURCE) public @interface SourceOnly {}

Can annotation parameters be of any type?

No, acceptable types are only primitives, strings, enums, other annotations, and arrays of these types. Objects cannot be used.

Can annotations be inherited?

Strictly speaking, annotations are not inherited like classes. The exception is an annotation with @Inherited, which only applies to subclasses (not affecting methods).

Typical mistakes and anti-patterns

  • Incorrect usage of retention and target order
  • Lack of documentation when creating custom annotations
  • Excessive annotation complicating code maintenance

Real-life example

Negative case

A developer in a large project created dozens of different annotations for test automation without bothering to write them down, document, and limit their scope. As a result, a new team member found it difficult to understand where different annotations worked.

Pros:

  • Flexible automation
  • Possibility for further expansion

Cons:

  • Confusion in code maintenance
  • Increased complexity of training new employees

Positive case

The team implemented a strict strategy for writing their annotations: they added only what was necessary, described each in detail, and limited their scope.

Pros:

  • High maintainability
  • Ease of support and training

Cons:

  • Time was needed for agreement and standardization