MyNameIsHans
MyNameIsHans

Reputation: 684

Should you store fields and their names in a Map when using reflection?

I am using reflection to get certain fields by their names and I'm accessing these fields rather often. Is it good to store the fields in a HashMap<String, Field> if I found them once and then get them from that HashMap when I need them again? Or does java do something similar already and this would by totally unneccesary?

Upvotes: 1

Views: 61

Answers (1)

J&#252;rgen
J&#252;rgen

Reputation: 418

This is a valid approach as there is no automated "caching" for reflection. As each reflective call consumes time caching on your own is always a good idea.

Upvotes: 1

Related Questions