Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Java syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Annotations==== {{Main|Java annotation}} Annotations in Java are a way to embed [[metadata]] into code. This language feature was introduced in [[J2SE 5.0]]. =====Annotation types===== Java has a set of predefined annotation types, but it is allowed to define new ones. An annotation type declaration is a special type of an interface declaration. They are declared in the same way as the interfaces, except the <code>interface</code> keyword is preceded by the <code>@</code> sign. All annotations are implicitly extended from <code>java.lang.annotation.Annotation</code> and cannot be extended from anything else. <syntaxhighlight lang="java"> @interface BlockingOperations { } </syntaxhighlight> Annotations may have the same declarations in the body as the common interfaces, in addition they are allowed to include enums and annotations. The main difference is that abstract method declarations must not have any parameters or throw any exceptions. Also they may have a default value, which is declared using the <code>default</code> keyword after the method name: <syntaxhighlight lang="java"> @interface BlockingOperations { boolean fileSystemOperations(); boolean networkOperations() default false; } </syntaxhighlight> =====Usage of annotations===== Annotations may be used in any kind of declaration, whether it is package, class (including enums), interface (including annotations), field, method, parameter, constructor, or local variable. Also they can be used with enum constants. Annotations are declared using the <code>@</code> sign preceding annotation type name, after which element-value pairs are written inside brackets. All elements with no default value must be assigned a value. <syntaxhighlight lang="java"> @BlockingOperations(/*mandatory*/ fileSystemOperations, /*optional*/ networkOperations = true) void openOutputStream() { //Annotated method } </syntaxhighlight> Besides the generic form, there are two other forms to declare an annotation, which are shorthands. ''Marker annotation'' is a short form, it is used when no values are assigned to elements: <syntaxhighlight lang="java"> @Unused // Shorthand for @Unused() void travelToJupiter() { } </syntaxhighlight> The other short form is called ''single element annotation''. It is used with annotations types containing only one element or in the case when multiple elements are present, but only one elements lacks a default value. In single element annotation form the element name is omitted and only value is written instead: <syntaxhighlight lang="java"> /* Equivalent for @BlockingOperations(fileSystemOperations = true). networkOperations has a default value and does not have to be assigned a value */ @BlockingOperations(true) void openOutputStream() { } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)