Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass variable as annotation parameter

I am using annotation in my java program something like this.

@annotation("some string")
public void fun(){
...
} 

Is there any way that i could pass variable instead of "some string" to annotation.

e.g

String s="some string"
@annotation(s)
public void fun(){
...
}
like image 271
Shubham Randive Avatar asked Jul 01 '26 19:07

Shubham Randive


1 Answers

It is not possible to give an annotation a changing variable. The value which is passed in to the annotation needs to be known at compile time.

This would work:

private final String param = "Param";

@annotation(param)
public void function() {

}

However, it must be constant and cannot be changed, e.g. intialized by the constructor. (The value in this case would be known at runtime, not compile time)

like image 196
9636Dev Avatar answered Jul 04 '26 10:07

9636Dev