Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

JNI: reuse an object

Thread view: 
Ken Kahl - 10 Mar 2006 17:41 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?

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.