What is the 'correct' way to store a native pointer inside a Java object?

Daniel Cassidy picture Daniel Cassidy · Dec 3, 2008 · Viewed 9.1k times · Source

What is the 'correct' way to store a native pointer inside a Java object?

I could treat the pointer as a Java int, if I happen to know that native pointers are <= 32 bits in size, or a Java long if I happen to know that native pointers are <= 64 bits in size. But is there a better or cleaner way to do this?

Edit: Returning a native pointer from a JNI function is exactly what I don't want to do. I would rather return a Java object that represents the native resource. However, the Java object that I return must presumably have a field containing a pointer, which brings me back to the original question.

Or, alternatively, is there some better way for a JNI function to return a reference to a native resource?

Answer

Tom Hawtin - tackline picture Tom Hawtin - tackline · Dec 3, 2008

IIRC, both java.util.zip and java.nio just use long.