Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my annotation: 

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface CacheClear {

    long versionId() default 0;

}

I want to pass the method param "versionTo" to the custom annotation. Below is my code:

@CacheClear(versionId = {versionTo})

public int importByVersionId(Long versionTo){

    ......

Can anyone tell me how to do it?

2 Answers

0 votes
by (19.7k points)

You can’t do it with custom annotations. You need constant values and a dynamic method parameter for annotations. 

Interested in Java? Check out this Java tutorial by Intellipaat.

0 votes
by (1.8k points)
We can pass a custom annotation in java by setting the values and defining elements in the annotations by:

Define a custom annotation that includes elements as properties. Apply this annotation by assigning values to each element as needed. If necessary, use reflection to retrieve these values at runtime. This method allows data to be stored within annotations and accessed later in your Java application.

Related questions

0 votes
1 answer
0 votes
1 answer

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...