|
Java Developers Desk:
Anotations |
|
An Introduction to Annotations
Sun Microsystem added the features like
annotation to make the development easier and
more efficient in jdk 5. The main objective to
develop the annotations is to make the
development easier. Annotations behaves like
the meta. The literal meaning of meta data is
data about data. Java also signifies this
meaning. Annotations are like meta data, means
you are free to add your code and can also
apply them to variables, parameters, fields type
declarations, methods and constructors.
Metadata is also used to create the
documentation to perform rudimentary compile
time checking and even for tracking down the
dependencies in code. XDoclet contains all these
features and is widely used. Annotations provide
a means of indicating about methods, classes,
dependencies, incompleteness and also about
the references on other methods and classes
respectively. Quoting from Suns official site,
It (annotation-based development) lets us
avoid writing boilerplate code under many
circumstances by enabling tools to generate it
from annotations in the source code. This leads
to a declarative programming style where the
programmer says what should be done and
tools emit the code to do it.
Annotation is the way of associating the
program elements with the meta tags so that
the compiler can extract program behavior to
support the annotated elements to generate
interdependent code when necessary.
Fundamentals of annotations
While going through the annotations you
should consider two things. The first one is
the annotation itself and second one is the
annotations types. An annotation is the
meta tag, used to give some life to the code
you are using. While annotation type is used
to define annotations so that you can use them
while creating your own custom annotations.
An annotation type definition appends an at
@ sign at the start of the interface keyword
with the annotation name. On the other hand,
an annotation includes the at @ sign followed
by the annotation type. You can also add the |
|
data within the parenthesis after the annotation
name. Lets illustrate the concept more clearly
by using some examples.
Defining an annotation (Annotation type)
public @interface Example {
String showSomething();
}
Annotating the code (Annotation)
Example (showSomething=”Hi! How r you”)
public void anymethod() {
....
} |
Annotation Types:
Three types of annotations types are there in
java.
I. Marker:
Like the marker interface, marker annotations
does not contain any elements except the name
itself. The example given below clarifies the
concept of marker interface.
Example:
public @interface Example{
}
Usage:
@Example
public void anymethod() {
——————
} |
II. Single-value:
This type of elements provide only single value.
It means that these can be represented with
the data and value pair or we can use the
shortcut syntax (just by using the value only
within the parenthesis).
Example:
public @interface Example{
String showSomething();
} |
|
|
Jan 2008 | Java Jazz Up | 7 |
|
|
|
View All Topics |
All Pages of this Issue |
Pages:
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53 ,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63 ,
64,
65 ,
66 ,
67 ,
68 ,
69 ,
70 ,
71 ,
72 ,
73 ,
74 ,
75 ,
76 ,
77 ,
78 ,
79 ,
80 ,
81 ,
82 ,
83,
84 ,
85 ,
86,
87 ,
88,
89 ,
90 ,
91 ,
92 ,
93 ,
94 ,
95 ,
96 ,
97 ,
98 ,
99 ,
100 ,
101 ,
102 ,
103,
104 ,
105 ,
106,
107,
Download PDF |
|
|
|
|
|
|
|
|
|