क्या मेरे पास दो जावा क्लास हैं जिनके पास सी कोड संकलित करने के लिए देशी कॉल हैं और मैं उन वर्गों को किसी अन्य वर्ग में कॉल करता हूं? उदाहरण के लिए मेरे पास क्लास ए और क्लास बी है, दोनों मूल कार्यों के लिए कॉल करते हैं।जावा जेएनआई लाइब्रेरी लोड करने के लिए कॉल करें
public class A{
// declare the native code function - must match ndkfoo.c
static {
System.loadLibrary("ndkfoo");
}
private static native double mathMethod();
public A() {}
public double getMath() {
double dResult = 0;
dResult = mathMethod();
return dResult;
}
}
public class B{
// declare the native code function - must match ndkfoo.c
static {
System.loadLibrary("ndkfoo");
}
private static native double nonMathMethod();
public B() {}
public double getNonMath() {
double dResult = 0;
dResult = nonMathMethod();
return dResult;
}
}
कक्षा सी तो दोनों कहता है, के बाद से वे दोनों पुस्तकालय लोड करने के लिए एक स्थिर कॉल करने वर्ग सी में उस बात करेंगे: वे इस तरह सेटअप कर रहे हैं? या यह, वर्ग जो लाइब्रेरी का उपयोग करता है करने के लिए कक्षा सी कॉल System.loadLibrary (...?
public class C{
// declare the native code function - must match ndkfoo.c
// So is it beter to declare loadLibrary here than in each individual class?
//static {
// System.loadLibrary("ndkfoo");
//}
//
public C() {}
public static void main(String[] args) {
A a = new A();
B b = new B();
double result = a.getMath() + b.getNonMath();
}
}