Hello,
I am using JNI to use some functionality of a c++ program. From java I
call a native method, which creates some c++ objects. Is it possible to
reuse these objects, if I call the native method a second time? And how
does it work?
Thanks.
Ken
Gordon Beaton - 10 Mar 2006 17:53 GMT
> I am using JNI to use some functionality of a c++ program. From java
> I call a native method, which creates some c++ objects. Is it
> possible to reuse these objects, if I call the native method a
> second time? And how does it work?
JNI doesn't care or even know about any C++ objects you create. You
are free to reuse them as many times as you like, but you need to
manage them yourself.
How you keep track of them between calls from your Java application is
up to you, a static global table in your native code is one simple
way.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Thomas Fritsch - 10 Mar 2006 18:22 GMT
> Hello,
>
> I am using JNI to use some functionality of a c++ program. From java I
> call a native method, which creates some c++ objects. Is it possible to
> reuse these objects, if I call the native method a second time? And how
> does it work?
Of course. Just keep a static C++ pointer pointing to your newly created C++
object.
static MyCPlusPlusObject* pObj = NULL;
your_jni_function(...) {
if (pObj == NULL)
pObj = new MyCPlusPlusObject();
}
Or alternatively, you can store a C++ pointer into a Java member variable of
type int (by JNI-function SetIntField). And later you can get it back with
GetIntField.

Signature
"Thomas:Fritsch$ops.de".replace(':', '.').replace('$', '@')
Ken Kahl - 11 Mar 2006 13:07 GMT
>>Hello,
>>
[quoted text clipped - 11 lines]
> pObj = new MyCPlusPlusObject();
> }
Thank you very much. It works in this way.
Ken