Telling Java you want an object to be permanent

If you have a reference to a Java™ object that was either passed to you as a parameter or was created by calling a Java method or constructor, and you want to use that object after your native method returns, you must tell Java that you want the object to be permanent, or "global". Do this by calling the RPG wrapper procedure getNewGlobalRef and saving the result in a global variable.
EVAL   globalString = getNewGlobalRef (JNIENV_P : string);

Figure 1 contains the sample source code for getNewGlobalRef.

Figure 1. Source Code for getNewGlobalRef
/*------------------------------------------------------*/
/* getNewGlobalRef                                      */
/*------------------------------------------------------*/
P getNewGlobalRef...
P                 B                    EXPORT
D getNewGlobalRef...
D                 PI              O    CLASS(*JAVA
D                                          : 'java.lang.Object')
D   env                           *    VALUE
D   localRef                      O    CLASS(*JAVA
D                                          : 'java.lang.Object')
D                                      VALUE

 /free
     jniEnv_P = env;
     return NewGlobalRef (env : localRef);
 /end-free
P getNewGlobalRef...
P                 E
Note: You need the JNI environment pointer (described in Obtaining the JNI environment pointer below) to call this wrapper.

Feedback