⬅ Previous Topic
Java throws KeywordNext Topic ⮕
Java Logging⬅ Previous Topic
Java throws KeywordNext Topic ⮕
Java LoggingJava Annotations are one of those features that quietly power many of Java’s most powerful frameworks—like Spring, Hibernate, and JUnit—yet they often get misunderstood or overlooked by beginners. In this tutorial, we’ll explore what annotations are, why they exist, and how to use them effectively in your programs.
Annotations in Java are a special kind of metadata that provide information about the code but do not directly affect its execution. They act like notes to the compiler or runtime, guiding them on how to treat the annotated elements (classes, methods, variables, etc.).
Before annotations, developers relied heavily on XML configuration files or marker interfaces. Annotations offer:
Java provides several built-in annotations, especially useful for compile-time checks.
Indicates that a method is intended to override a method in a superclass.
class Animal {
void speak() {
System.out.println("Animal speaks");
}
}
class Dog extends Animal {
@Override
void speak() {
System.out.println("Dog barks");
}
}
Dog barks
Marks a method or class as outdated, warning developers to avoid using it.
class LegacyCode {
@Deprecated
void oldMethod() {
System.out.println("Old method, avoid using.");
}
}
Instructs the compiler to suppress specific warnings.
@SuppressWarnings("unchecked")
List list = new ArrayList();
Java also provides meta-annotations that annotate other annotations:
@Target
– Specifies the kinds of elements an annotation can be applied to.@Retention
– Indicates how long annotations are retained (SOURCE, CLASS, or RUNTIME).@Documented
– Marks an annotation for inclusion in the generated JavaDoc.@Inherited
– Allows subclasses to inherit the annotation from a superclass.Let’s see how you can define your own annotation:
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface LogExecutionTime {
}
Now, apply this annotation to a method:
class Demo {
@LogExecutionTime
void run() {
System.out.println("Running method...");
}
}
Using Java Reflection API, we can process annotations at runtime.
import java.lang.reflect.Method;
class AnnotationProcessor {
public static void main(String[] args) throws Exception {
Demo d = new Demo();
Method m = d.getClass().getMethod("run");
if (m.isAnnotationPresent(LogExecutionTime.class)) {
long start = System.currentTimeMillis();
m.invoke(d);
long end = System.currentTimeMillis();
System.out.println("Execution time: " + (end - start) + "ms");
}
}
}
Running method...
Execution time: Xms
Annotations are not just academic—they are used widely in:
@Autowired
, @Component
, @Service
@Entity
, @Table
, @Id
@Test
, @Before
, @After
@Override
annotation?@SuppressWarnings
annotation changes how the JVM executes the code.⬅ Previous Topic
Java throws KeywordNext Topic ⮕
Java LoggingYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.