Jump to content
  • Advertisement
Sign in to follow this  
LizardCPP

[java] JNI and returning cutom classes

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!