[java] JNI and returning cutom classes

Started by
1 comment, last by LizardCPP 18 years, 6 months ago
Hey all. I want to do something like this:

public class MyClass
{
     private String name;

     public MyClass(String name)
     {
        this.name = name;
     }

     public String getName()
     {
        return nmae;
     }

}


public class NativeInterface
{
   public native MyClass getMyClass();

   public static void main(String[] args)
   {
       NativeInterface nativeInterface = new Native_Interface();
       MyClass myClass = nativeInterface.getMyClass();
       System.out.println(myClass.getName());
   }
}




Can this be done? Or can I only return primitive types (i.e. int, short, String etc) through JNI? Lizard [Edited by - LizardCPP on September 24, 2005 5:09:20 PM]
Advertisement
You can return non-primitive types through JNI too, but you have to get the references somehow.

I believe there are JNI functions for calling constructors so you can create new objects, and also for looking up existing objects off properties of other things.

So in this case (which of course, you'd never, ever do in JNI), you'd have to get the "name" property off this object.

Bear in mind that it's often easier, rather than making a public method API native, to write that in Java and have it call a private native method which does as little as possible, because it's so complicated / error-prone to do real work with Java objects in native code.

Mark
Ok thanks for a fast answer.

Lizard

This topic is closed to new replies.

Advertisement