4

To avoid having to create multiple instances of the org.reflections.Reflections class I was thinking of just creating one and reusing as needed. Anyone know if this class is thread safe?

If its not thread safe I know I can use Java's ThreadLocal wrapper (in case anyone was going to respond with that).

Jose Martinez
  • 11,452
  • 7
  • 53
  • 68
  • Can you elaborte? What is the motivation; What is the error. – zapp Jan 26 '15 at 14:15
  • No error, at least not yet. I have created a utils class for all types of reflection needs that users may need. This utils class is going to be shared via a commons library we use in all our projects. I want to know if it is ok to have the Reflection object be static so that I do not have to create them often. – Jose Martinez Jan 26 '15 at 14:20

1 Answers1

1

You can definitely create only one instance, and use it many times.

The rational is that the Reflection's storage is only populated at scan time (that is, when instantiating it), and queries later on are read only, with defensive copy semantics.

zapp
  • 1,533
  • 1
  • 14
  • 18