JNI for a nested class

A

Aaron Fude

Hi,

I use something like this to create an instance of "MyClass":

jclass clsMyClass = env->FindClass( "mypackage/MyClass" );
jmethodID midCtor = env->GetMethodID( clsMyClass, "<init>", "()V" );
jobject myInst = env->NewObject( clsMyClass, midCtor );


However, if I try a similar approach for create an instance of
MyClass.Nested where the nested class is static, of course, I get a crash. I
do this:

clsMyClass = env->FindClass( "mypackage/MyClass.Nested" );

Is this the correct syntax?
Is this possible in principle?
Should I post more detail?

Very many thanks in advance!

Aaron Fude
 
A

Aaron Fude

OK, figured it out 5 sec after the post when I decided to give javap a
chance and realized that the class name has a $ in it. So the correct syntax
is

clsMyClass = env->FindClass( "mypackage/MyClass$Nested" );

Is this correct?
 
G

Gordon Beaton

OK, figured it out 5 sec after the post when I decided to give javap
a chance and realized that the class name has a $ in it. So the
correct syntax is

clsMyClass = env->FindClass( "mypackage/MyClass$Nested" );

Is this correct?

So far, yes.
jmethodID midCtor = env->GetMethodID( clsMyClass, "<init>", "()V" );
jobject myInst = env->NewObject( clsMyClass, midCtor );

A non-static inner class requires an instance of the surrounding
class, which you need to consider when looking up and invoking the
constructor.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top