Reputation: 376
Will FindClass succeed only if an instance of the class has previously been instantiated?
If so, what it the lowest-cost way to instantiate a throw-away instance of a class so that a subsequent call to FindClass will succeed?
Or, is there another JNI method that will work with uninstantiated classes?
(In my case, the class I'm trying to Find just has static methods. I would like my native code to be able to call one of these Java class static methods.)
-Allan
Upvotes: 1
Views: 801
Reputation: 7293
Will FindClass succeed only if an instance of the class has previously been instantiated?
No. It will find any class which your application's classloader knows about. Instantiated or not.
As long as only static methods are called and static class members are used, instance won't get created. Java is quite lazy in creating instances. Even the static initializer block is executed later than expected, see get static initialization block to run in a java without loading the class
When you read JNI documentation carefully, you will find out that CallStatic<type>Method
family takes jclass
as a parameter, whereas Call<type>Method
takes jobject
. I think this difference explains all.
On a bottom note: there is nothing like "throw-away instance" in Java. You probably think about C++ style, scoped instances created on stack. Java cannot be commanded to do that, everything is allocated dynamically on heap and scope is determined by refcounting and scheduling of garbage collection.
Upvotes: 1