How to Use Retention Policy in Java Annotations


The example is to show how to use Retention policy in Java annotations.

Source Code

package com.beginner.examples;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
 

 
public class RetentionPolicyExample{
	
	@Retention(RetentionPolicy.RUNTIME)
	public @interface Test {
	 
	    String name();
	}
     
    @Test(name = "test")
    public static String test(){
        //method implementation
    	return Test.class.getSimpleName();
    }
    public static void main(String[] args){
    	System.out.println(test());
    }
}

Output:

Test

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments