Stay organized with collections
Save and categorize content based on your preferences.
Target
public
abstract
@interface
Target
implements
Annotation
java.lang.annotation.Target
|
Indicates the contexts in which an annotation interface is applicable. The
declaration contexts and type contexts in which an annotation interface may
be applicable are specified in JLS {@jls 9.6.4.1}, and denoted in source code by
enum constants of java.lang.annotation.ElementType
.
If an @Target
meta-annotation is not present on an annotation
interface T
, then an annotation of type T
may be written as
a modifier for any declaration.
If an @Target
meta-annotation is present, the compiler will enforce
the usage restrictions indicated by ElementType
enum constants, in line with JLS {@jls 9.7.4}.
For example, this @Target
meta-annotation indicates that the
declared interface is itself a meta-annotation interface. It can only be
used on annotation interface declarations:
@Target(ElementType.ANNOTATION_TYPE)
public @interface MetaAnnotationType {
...
}
This @Target
meta-annotation indicates that the declared class or
interface is intended solely for use as a member class or interface in
complex annotation interface declarations. It cannot be used to annotate
anything directly:
@Target({})
public @interface MemberInterface {
...
}
It is a compile-time error for a single ElementType
constant to
appear more than once in an @Target
annotation. For example, the
following @Target
meta-annotation is illegal:
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})
public @interface Bogus {
...
}
Summary
Public methods |
ElementType[]
|
value()
Returns an array of the kinds of elements an annotation interface
can be applied to.
|
Public methods
value
public ElementType[] value ()
Returns an array of the kinds of elements an annotation interface
can be applied to.
Returns |
ElementType[] |
an array of the kinds of elements an annotation interface
can be applied to |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# Target\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \nSummary: [Methods](#pubmethods) \\| [Inherited Methods](#inhmethods) \n\nTarget\n======\n\n\n`\npublic\n\n\nabstract\n@interface\nTarget\n`\n\n\n`\n\n\nimplements\n\n`[Annotation](/reference/java/lang/annotation/Annotation)`\n\n\n`\n\n|-----------------------------|\n| java.lang.annotation.Target |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nIndicates the contexts in which an annotation interface is applicable. The\ndeclaration contexts and type contexts in which an annotation interface may\nbe applicable are specified in JLS {@jls 9.6.4.1}, and denoted in source code by\nenum constants of [java.lang.annotation.ElementType](/reference/java/lang/annotation/ElementType).\n\nIf an `@Target` meta-annotation is not present on an annotation\ninterface `T`, then an annotation of type `T` may be written as\na modifier for any declaration.\n\nIf an `@Target` meta-annotation is present, the compiler will enforce\nthe usage restrictions indicated by `ElementType`\nenum constants, in line with JLS {@jls 9.7.4}.\n\nFor example, this `@Target` meta-annotation indicates that the\ndeclared interface is itself a meta-annotation interface. It can only be\nused on annotation interface declarations: \n\n```\n @Target(ElementType.ANNOTATION_TYPE)\n public @interface MetaAnnotationType {\n ...\n }\n \n```\n\nThis `@Target` meta-annotation indicates that the declared class or\ninterface is intended solely for use as a member class or interface in\ncomplex annotation interface declarations. It cannot be used to annotate\nanything directly: \n\n```\n @Target({})\n public @interface MemberInterface {\n ...\n }\n \n```\n\nIt is a compile-time error for a single `ElementType` constant to\nappear more than once in an `@Target` annotation. For example, the\nfollowing `@Target` meta-annotation is illegal: \n\n```\n @Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})\n public @interface Bogus {\n ...\n }\n \n```\n\n\u003cbr /\u003e\n\nSummary\n-------\n\n| ### Public methods ||\n|-----------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` `[ElementType[]](/reference/java/lang/annotation/ElementType) | ` `[value](/reference/java/lang/annotation/Target#value())`() ` Returns an array of the kinds of elements an annotation interface can be applied to. |\n\n| ### Inherited methods |\n|-----------------------|---|\n| From interface ` `[java.lang.annotation.Annotation](/reference/java/lang/annotation/Annotation)` ` |-------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract `[Class](/reference/java/lang/Class)`\u003c? extends `[Annotation](/reference/java/lang/annotation/Annotation)`\u003e` | ` `[annotationType](/reference/java/lang/annotation/Annotation#annotationType())`() ` Returns the annotation interface of this annotation. | | ` abstract boolean` | ` `[equals](/reference/java/lang/annotation/Annotation#equals(java.lang.Object))`(`[Object](/reference/java/lang/Object)` obj) ` Returns true if the specified object represents an annotation that is logically equivalent to this one. | | ` abstract int` | ` `[hashCode](/reference/java/lang/annotation/Annotation#hashCode())`() ` Returns the hash code of this annotation. | | ` abstract `[String](/reference/java/lang/String) | ` `[toString](/reference/java/lang/annotation/Annotation#toString())`() ` Returns a string representation of this annotation. | ||\n\nPublic methods\n--------------\n\n### value\n\nAdded in [API level 1](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic ElementType[] value ()\n```\n\nReturns an array of the kinds of elements an annotation interface\ncan be applied to.\n\n\u003cbr /\u003e\n\n| Returns ||\n|--------------------------------------------------------------|------------------------------------------------------------------------------------|\n| [ElementType[]](/reference/java/lang/annotation/ElementType) | an array of the kinds of elements an annotation interface can be applied to \u003cbr /\u003e |"]]