- Home /
What is the difference between AndroidJavaClass.Call and AndroidJavaObject.Call
According to the documentation:
AndroidJavaObject is the Unity representation of a generic instance of java.lang.Object. AndroidJavaClass is the Unity representation of a generic instance of java.lang.Class.
Also, AndroidJavaClass derives from AndroidJavaObject.
What is the difference between these two:
AndroidJavaClass ajc = new AndroidJavaClass("someclass"); AndroidJavaObject ajo = new AndroidJavaObject("someclass");
ajo.Call("Method"); // Calls an instance method using the jobject reference
ajc.Call("Method"); // Calls an instance method using the class reference (?? doesn't make sense)
Answer by liortal · May 28, 2014 at 08:36 PM
Answering my own question - after doing some research, it seems that the two methods are the same.
AndroidJavaClass inherits from AndroidJavaObject, but does not override the Call method. This means the Call method for this class is exactly the same one as for AndroidJavaObject.
Note that when using an AndroidJavaClass, using the Call method has no effect, since no object instance was initialized (one can only use CallStatic or the generic CallStatic).
I am currently researching on that since I'm getting stuck in a mail sending problem. From what I have read in the docs (althoug I haven't tried it yet), you could be able to use AndroidJavaClass.Call on a static class pretty much as you can do it with a regular C# class that doesn't derive from $$anonymous$$onoBehaviour. Am I too wrong? I'm not a Java coder (much less an experienced android developer).
Answer by tyoc213 · Aug 06, 2014 at 03:09 PM
From http://www.j2megame.com/html/xwzx/ty/2609.html
This will not create another com.unity3d.player.UnityPlayer Activity (of some sort)... it will let you directly access the class static methods without create "new instance of the class".
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
// jni.FindClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
// jni.GetStaticFieldID(classID, "Ljava/lang/Object;");
// jni.GetStaticObjectField(classID, fieldID);
// jni.FindClass("java.lang.Object");
This will create a new java string on VM.
AndroidJavaObject jo = new AndroidJavaObject("java.lang.String", "some_string");
// jni.FindClass("java.lang.String");
// jni.GetMethodID(classID, "<init>", "(Ljava/lang/String;)V");
// jni.NewStringUTF("some_string");
// jni.NewObject(classID, methodID, javaString);
As I see any other case is the same usage or equivalent. Basically use AndroidJavaClass when going to call static methods and not want to create a new instance, NOTE that you can also access static methods from AndroidJavaObject created instance.
But i was specifically interested in whats the different between both classes' Call method.
Your answer
Follow this Question
Related Questions
AndroidJavaClass. Call returns null ptr. 1 Answer
AndroidJavaObject.Call fails with 'method not found' 1 Answer
Call non-static method (from subclass) in Java from Unity 1 Answer
Getting native Android String constants via JNI broken in Unity 2019.2.0? 3 Answers
Returning byte array from Java/Android 3 Answers