r/java • u/munklers • Jul 28 '26
Why Are Method References not allowed in Annotations in Java?
Many times I have wanted to refer to a method in Java in an annotation, but it seems annotations can only reference items from the Class constant pool. It seems like it would have been a natural extension to be able to refer statically to methods (like {@link} in javadocs) in annotations, but they were left.
I'm wondering why were they not added?
32
Upvotes
View all comments
Show parent comment
2
u/brian_goetz Aug 03 '26
And the story hasn't changed since then.
(Unbound) Method refs in annotations are semantically sensible, as they are "constants". But there is so much acccidental complexity in the design and implementation of annotations (it was never designed to be extended) and it involves intrusive changes to many layers, that the payoff would have to be huge, and the payoff is merely "decent".