🧩 Introduction
Java’s annotation system is one of the most powerful yet underrated features of the language. It allows developers to embed metadata into code and use it to control behavior dynamically at runtime. Frameworks like Spring, Hibernate, and JUnit heavily rely on annotations and Java Reflection to achieve flexibility and automation.
In this blog, we’ll explore how annotations work under the hood, how to create your own custom annotations, and how to use reflection to build simple framework-like functionality from scratch.
🧠 Understanding Java Annotations
Annotations in Java are special markers that provide metadata about your code but do not directly affect its execution. They can be used to give instructions to the compiler, generate code, or modify runtime behavior.
Example of a built-in annotation:
@Override
public String toString() {
return "Custom Object";
}
Annotations can have retention policies:
- SOURCE: Used only by the compiler, discarded at runtime.
- CLASS: Stored in the class file but not available at runtime.
- RUNTIME: Retained and accessible via reflection — perfect for frameworks.
⚙️ Creating Custom Annotations
Here’s how you can define your own annotation:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RunTest {
String value() default "Default Test";
}
This custom annotation can now be used to mark specific methods:
public class TestSuite {
@RunTest("Login Test")
public void login() {
System.out.println("Running login test...");
}
}
🔍 Using Reflection to Build a Mini Framework
Reflection allows your code to inspect and manipulate classes, methods, and fields at runtime.
Here’s a simple example of how a framework might detect and execute annotated methods:
for (Method method : TestSuite.class.getDeclaredMethods()) {
if (method.isAnnotationPresent(RunTest.class)) {
method.invoke(new TestSuite());
}
}
This mimics how testing frameworks like JUnit automatically detect and run tests annotated with @Test.
🚀 Conclusion
Custom annotations and reflection open the door to meta-programming — where your code adapts based on metadata. By mastering these features, you can build tools, frameworks, and libraries that make your applications more flexible, modular, and maintainable.
Whether you’re building a test runner, ORM mapper, or dependency injection container, understanding annotations and reflection is a vital skill for every Java developer.
This Content Sponsored by SBO Digital Marketing.
Mobile-Based Part-Time Job Opportunity by SBO!
Earn money online by doing simple content publishing and sharing tasks. Here's how:
- Job Type: Mobile-based part-time work
- Work Involves:
- Content publishing
- Content sharing on social media
- Time Required: As little as 1 hour a day
- Earnings: ₹300 or more daily
- Requirements:
- Active Facebook and Instagram account
- Basic knowledge of using mobile and social media
For more details:
WhatsApp your Name and Qualification to 9994104160
a.Online Part Time Jobs from Home
b.Work from Home Jobs Without Investment
c.Freelance Jobs Online for Students
d.Mobile Based Online Jobs
e.Daily Payment Online Jobs
Keyword & Tag: #OnlinePartTimeJob #WorkFromHome #EarnMoneyOnline #PartTimeJob #jobs #jobalerts #withoutinvestmentjob


No comments:
Post a Comment