Aşağıda @Deprecated ve @Override gibi öntanımlı annotation'ları (predefined annotation) kullanılan bir sınıf gözükmektedir:
package com.test.annotation;
public class PredefinedAnnotations {
int x=1;
public int getX(){
return x;
}
public void setX(int x){
this.x=x;
}
@Deprecated
public void setXValue(int x){
this.x=x;
}
@Override
public String toString() {
return "x:"+x;
}
}
setXValue method'u @Deprecated ile deprecated edilmiştir. toString method'unda ise Object sınıfın toString method'unun override edileceği belirtilmiştir. Örneğin yanlış olarak
@Override
public String tostring() {
return "x:"+x;
}
yazarsak, derleyici tostring adında override edilen bir method'un olmadığını söyler ve hata yapmanızı engeller:
The method tostring() of type PredefinedAnnotations must override or implement a supertype method
Yukarıdaki sınıfı aşağıdaki gibi kullanalım
package com.test.annotation;
public class OtherClass {
@SuppressWarnings("deprecation")
public OtherClass() {
PredefinedAnnotations predefinedAnnotations=new PredefinedAnnotations();
predefinedAnnotations.setXValue(15);
System.out.println(predefinedAnnotations.getX());
}
}
setXValue deprecated bir method olmasından dolayı derleyi uyarı verecektir. Bu uyarının verilmemesi için @SuppressWarnings("deprecation") eklenmiştir.