2

I want to cache method IDs across calls. For this I get method IDs:

// Put static to clarify it's saved across calls.
static jmethodID method = env->GetMethodID(class_HelloWorld, name, signature);

My question is: do I need to make class_HelloWorld a global ref with NewGlobalRef or method IDs can be obtained from local ref and saved (I get class_HelloWorld using FindClass) without need for global ref-ing the class metadata?

demi
  • 5,384
  • 6
  • 37
  • 57

1 Answers1

5

If the class you got the methodID from doesn't change, you can re-use the methodID. It isn't like a jclass or jobject that needs to be cached as a GlobalRef: it's just an integer really.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • The answer appears to be straight to the point, but how can classes possibly change? Java is not dynamic language and as far as I know it's not possible to modify class definition to add or remove methods. As I understand the question terms, there are [many](https://stackoverflow.com/a/13940735/213871) [answers](https://stackoverflow.com/a/19657117/213871) that also cache references to classes when the actual question was just how to cache method/field ids to reuse later. So the question really is: will method/fields ids be valid for all the duration of the program even without class caching? – ceztko Jun 03 '19 at 12:37
  • @certko Yes they will. – user207421 May 29 '23 at 23:48