In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to call static methods in different kinds of Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to call static methods in different kinds of Java +.
Preface
In the previous article, we introduced "Android NDK programming (4)-C _ Java + calling methods in Java", mainly the methods of calling Java in C & C +. This article extends to the content of the previous article, about adjusting static methods in different classes.
Code demonstration
Let's continue with the original Demo program, first create a new class named VaccaeJNI, and then add a static method of native to this class.
Add the corresponding method function to native-lib.cpp through ALT+ENTER, and then write the implementation method.
Finally, the static method in the VaccaeJNI class is called in our button event.
Running result
In the above, we basically have the same method of transferring CCompact + from java. Next we will focus on the method of calling java with CAccord +.
We add a native static method and a local static method to the VaccaeJNI.
Then the implementation method is written in the corresponding C++ file.
From the figure above, we can see that when we call static methods, our GetMethodID and CallMethod methods both add static to GetStaticMethodID and CallStaticIntMethod. If we don't change here, there will be an error in the call.
We write the implementation method again in the button event
It is relatively easy to call static methods. Let's take a look at the running results.
Call methods in different classes
Finally, we want to make a point, because when we write methods, we may call methods in different classes. The methods that navigate native and the java methods to be called are not in the same class. When we encounter such methods, let's take a look at how to implement them.
Let's write a non-static method in the class of VaccaeJNI, multiplying two numbers.
Then call the function of native, and we write it directly in Mainactivity.java.
In the figure above, we first define a parameter of plusFromJNI, enter two worthy returns, then write the button event to call the method, and then press ALT+ENTER to generate the method in the C++ file.
Native-lib.cpp
Extern "C" JNIEXPORT jint JNICALLJava_dem_vac_vaccaendk_MainActivity_plusFromJNI (JNIEnv * env, jobject instance, jint a, jint b) {
/ / first get class. The parameter jobject passed here represents Mainactivity, so we need to look for jclass jcls=env- > FindClass ("dem/vac/vaccaendk/VaccaeJNI"); if (jcls= = NULL) {return 0;}
/ / get method jmethodID jmethod= env- > GetMethodID (jcls, "Plus", "(II) I"); if (jmethod==NULL) {return 0;}
/ / call access / / the first parameter is jobject, which is passed in jobject is Mainactivity, / / if you directly use the passed in CallIntMethod, there will be an error, because our / / method is in the class of VaccaeJNI, so we need to instantiate that class first, and then call jobject jobj = env- > AllocObject (jcls); return env- > CallIntMethod (jobj,jmethod,a,b)
}
In the above code, we should mainly focus on a few places where I have written comments, as shown in the following figure
Finally, let's run the program again to see the results.
At this point, I believe that everyone has a deeper understanding of "how to call static methods in different kinds of Java", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.