Java Reference
In-Depth Information
An annotation is added in the same way you add a modifier for a program element. You can mix the annotation
for a program element with its other modifiers. You can place annotations in the same line as other modifiers or
in a separate line. It is a personal choice whether you use a separate line to place the annotations or you mix them
with other modifiers. By convention, annotations for a program element are placed before all other modifiers. Let's
follow this convention and place the annotation in a separate line by itself, as shown above. Both of the following
declarations are technically the same:
// Style #1
@Version(major=1, minor=0) public class VersionTest {
// Code goes here
}
// Style #2
public @Version(major=1, minor=0) class VersionTest {
// Code goes here
}
Listing 1-2 shows the sample code for the VersionTest class.
Listing 1-2. A VersionTest Class with Annotated Elements
// VersionTest.java
package com.jdojo.annotation;
// Annotation for class VersionTest
@Version(major = 1, minor = 0)
public class VersionTest {
// Annotation for instance variable xyz
@Version(major = 1, minor = 1)
private int xyz = 110;
// Annotation for constructor VersionTest()
@Version(major = 1, minor = 0)
public VersionTest() {
}
// Annotation for constructor VersionTest(int xyz)
@Version(major = 1, minor = 1)
public VersionTest(int xyz) {
this.xyz = xyz;
}
// Annotation for the printData() method
@Version(major = 1, minor = 0)
public void printData() {
}
// Annotation for the setXyz() method
@Version(major = 1, minor = 1)
public void setXyz(int xyz) {
Search WWH ::




Custom Search