Sudhanva Patil
Sudhanva Patil

Reputation: 61

I have a java annotation which takes a string constant as a argument , how can I Pass a string variable to the same?

For Eg @FindBy(how = How.XPATH, using="//input[@name='Username']") here I want to replace the value of the string constant "//input[@name='Username']" with a string variable , even though I declare the variable as final I am unable to pass the variable as a parameter. I want to write like this

final   static String Username_xpath="//input[@name='Username']";   
@FindBy(how = How.XPATH, using=Username_xpath)      

Upvotes: 2

Views: 6048

Answers (3)

BlackJoker
BlackJoker

Reputation: 3191

No,you can not use variable in Annotation,only constant is permitted.

the following will work,because here USERNAME_XPATH is a constant:

final static String USERNAME_XPATH="//input[@name='Username']";
@FindBy(how = How.XPATH, using=USERNAME_XPATH)

Upvotes: 6

vargapeti
vargapeti

Reputation: 301

Annotations are designed for compile-time or deployment-time processing. At this point you do not have any runtime variables. Therefore it is not possible to use variables in association with annotations.

Upvotes: 0

Uwe Plonus
Uwe Plonus

Reputation: 9974

You cannot use a variable as an annotation value because the annotation is already evaluated at compile time.

Therefore only compile time constants (static final) can be used as values in annotations.

Upvotes: 1

Related Questions