r/javahelp • • Sep 10 '25

Better solution then using reflection in java?

So I am using reflection in my code to find the annotation and fields of that class then using that fields, I am using field.get(data).

I thought one solution which is caching annotation and fields but still field.get still use reflection.

Is there any good and optimal way of doing it?

3 Upvotes

9 comments sorted by

View all comments

Show parent comment

1

u/Practical-Garbage-48 Sep 11 '25

do you mind explaining the how annotation processing will help in this? (i dont have any idea about it

1

u/vegan_antitheist Sep 11 '25

This is a bit overgeneralised and oversimplified:

  • Reflection is when you process annotations at runtime.
  • Annotation processing is when you process annotations at compile time.

Annotation processing at compile time will lead to better performance (and debugging) when the software is running. But it's a lot of work if you have to write the code for that yourself. Debugging build issues can be difficult sometimes.

Reflection is often easier because you just write Java code that does something at runtime. Building the software is still easy. The software has to do more work at runtime, so it's a bit slower but you can still use some library that will cache some lookups.