-1

If I had two instances of a class instantiated in separate threads with the same annotation. And both threads were to modify the contents of the annotation for their class. Will the contents of the annotation be different for each instance or the same?

To set some context I am using testng and cucumber and would like to run the same test method twice with different tags. I was planning on editing the cucumber options at runtime via reflection and was sort of assuming that as they are in separate threads that would be fine (im pretty sure that was a dumb assumption)...

I'd seen the following example on how to modify the params of annotations at runtime

blablabla
  • 304
  • 1
  • 8
  • 18
  • 1
    How would you plan on modifying an annotation at runtime? Annotations are defined at compile-time only, stored in the class file and instances of `Annotation` are immutable. – Radiodef Jun 20 '18 at 20:40
  • just added an example. There's a function that uses the data of the annotation later on in the code I'm mentioning. – blablabla Jun 20 '18 at 20:52

1 Answers1

0

An instance of a class only creates new field primitives or references on the heap; it does not modify the class template. If you use reflection to modify the annotations, you are modifying the class template, even if the annotations are on fields.

Threads should have no impact, apart from setting up race conditions if you do reflection in them.

Apparently, you have written code to do this and are seeing some sort of results; are they consistent with that explanation?

Steve11235
  • 2,849
  • 1
  • 17
  • 18
  • Thanks, this makes sense and is pretty much consistent with what I see. I'll probably try and edit what I'm doing to edit the parameters of the function that uses the annotation rather than the annotation itself – blablabla Jun 20 '18 at 21:21