ElementType.TYPE: Annotating Classes and Interfaces
What you'll learn: How to use ElementType.TYPE to apply annotations to classes, interfaces, enums, and records.
What Is ElementType.TYPE?
When you create a custom annotation, you use @Target to specify where that annotation can be applied. ElementType.TYPE is the broadest declaration-level target—it allows your annotation to be placed on any type declaration: classes, interfaces (including annotation interfaces), enums, and records.
Think of ElementType.TYPE as a "stamp of approval" for entire blueprints. Just as a building inspector might mark an entire architectural plan as approved, TYPE lets you annotate the top-level structure of your code components.
Defining a TYPE-Targeted Annotation
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Entity {
String tableName();
}
Now your @Entity annotation can only be applied to type declarations—not methods, fields, or parameters.
Using TYPE Annotations
@Entity(tableName = "users")
public class User {
// class body
}
@Entity(tableName = "roles")
public interface Role {
// interface methods
}
@Entity(tableName = "status_codes")
public enum Status {
ACTIVE, INACTIVE
}
All three declarations are valid because they're all types. The annotation documents metadata about the entire structure.
Why TYPE Matters
ElementType.TYPE is essential for framework-level annotations that describe the purpose or behavior of an entire class or interface. ORM frameworks use it for entity mappings, dependency injection frameworks use it to mark components, and serialization libraries use it to control class-level behavior.
Key Takeaway: Use @Target(ElementType.TYPE) when your annotation describes or configures an entire class, interface, enum, or record—not individual members within them.