📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
What is Annotation?
- Inform the compiler about warnings and errors
- Manipulate source code at compilation time
- Modify or examine behavior at runtime
Java Built-in Annotations from java.lang Package
- @Deprecated - A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists.
- @Override - Indicates that a method declaration is intended to override a method declaration in a supertype.
- @FunctionalInterface - An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification.
- @SafeVarargs - A programmer assertion that the body of the annotated method or constructor does not perform potentially unsafe operations on its varargs parameter.
- @SuppressWarnings - Indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element).
@Deprecated
// Javadoc comment follows
/**
* @deprecated
* explanation of why it was deprecated
*/
@Deprecated
static void deprecatedMethod() { }
}
@Override
package net.javaguides.annotations;
public class OverrideAnnotationExample {
public static void main(String[] args) {
BaseClass baseClass = new SubClass();
baseClass.test();
SubClass subClass = new SubClass();
subClass.test();
}
}
class BaseClass {
public void test() {
System.out.println("Inside baseclass");
}
}
class SubClass extends BaseClass {
@Override
public void test() {
System.out.println("Inside subclass");
}
}
Inside subclass
Inside subclass
@FunctionalInterface
@FunctionalInterface
interface Sayable{
void say(String msg); // abstract method
}
public class FunctionalInterfacesExample {
public static void main(String[] args) {
Sayable sayable = (msg) - > {
System.out.println(msg);
};
sayable.say("Say something ..");
}
}
@SafeVarargs
package net.javaguides.annotations;
import java.util.ArrayList;
import java.util.List;
public class SafeVarAnnotationExample {
@SafeVarargs
private void print(List...names) {
for (List < String > name: names) {
System.out.println(name);
}
}
public static void main(String[] args) {
SafeVarAnnotationExample obj = new SafeVarAnnotationExample();
List < String > list = new ArrayList < String > ();
list.add("Ramesh");
list.add("John");
obj.print(list);
}
}
[Ramesh, John]
@SuppressWarnings
@SuppressWarnings("unchecked")
void uncheckedGenerics() {
List words = new ArrayList();
words.add("hello"); // this causes unchecked warning
}
Annotations That Apply to Other Annotations
@Retention
- RetentionPolicy.SOURCE – The marked annotation is retained only in the source level and is ignored by the compiler.
- RetentionPolicy.CLASS – The marked annotation is retained by the compiler at compile time, but is ignored by the Java Virtual Machine (JVM).
- RetentionPolicy.RUNTIME – The marked annotation is retained by the JVM so it can be used by the runtime environment.
@Documented
@Target
- ElementType.ANNOTATION_TYPE can be applied to an annotation type.
- ElementType.CONSTRUCTOR can be applied to a constructor.
- ElementType.FIELD can be applied to a field or property.
- ElementType.LOCAL_VARIABLE can be applied to a local variable.
- ElementType.METHOD can be applied to a method-level annotation.
- ElementType.PACKAGE can be applied to a package declaration.
- ElementType.PARAMETER can be applied to the parameters of a method.
- ElementType.TYPE can be applied to any element of a class.
Comments
Post a Comment
Leave Comment