• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by brunoleos · Jan 06, 2017 at 09:54 AM · androidjavaobject

Why does AndroidJavaClass exist?

AndroidJavaClass just inherits AndroidJavaObject, and has no overrides. What is it use?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Julijana · May 29, 2018 at 05:04 PM 0
Share

excellent question

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jan 06, 2017 at 05:10 PM

I answered this question already on your comment over here. As i said both classes serve a different purpose. It does have an override, the constructor.

  • AndroidJavaClass is a wrapper class for a native Java class. All the constructor does is finding the Java class with the given name.

  • AndoidJavaObject is a wrapper class for a native Java class instance. The constructor of this class does create an instance of the Java class and serves as wrapper for that class instance.

Both classes have a distinct usage.
AndroidJavaClass allows you to access a Java class type and access static members of that class. In this case no instance of that class is created.
AndoidJavaObject will create an instance of a Java class and allows you to access instance members as well as static members.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image brunoleos · Jan 09, 2017 at 03:19 PM 0
Share

The constructor of AndroidJavaClass partially initializes the base class (AndroidJavaObject), as it lets a null pointer for the class instance (the object). So the specific need for AndroidJavaClass is just to get an exception if I try to access an instance member?

If that's the case, I see a bad architecture for AndroidJavaClass. I'm worried if I can have all the functionality using just AndroidJavaObject, which would make the code simpler and intuitive.

avatar image Bunny83 brunoleos · Jan 10, 2017 at 02:30 AM 0
Share

Are you sure you read my answer carefully? When you create an instance of "AndroidJavaObject" you actually create an instance of the wrapped class inside the Java V$$anonymous$$ which is not the case when you use AndroidJavaClass. You seem to always concentrate on the irrelevant things and ignore the actual reason for the two classes. You can not use AndroidJavaObject directly to get access to a Java class without creating an instance of that Java class.

Also keep in $$anonymous$$d that both classes are just wrappers around the JNI bridge. You don't have to use those two classes at all, but they simplify the usage of the JNI bridge by a lot. But if you want, just use AndroidJNI directly. Though most methods of AndroidJavaObject and AndroidJavaClass would require several JNI calls.

avatar image brunoleos Bunny83 · Jan 10, 2017 at 12:47 PM 0
Share

Hi Bunny83, I'm carefully studying this problem, and your comments, you don't need to worry about this. The case is that AndroidJavaObject also wraps a java.lang.class object. This is accessible with GetRawClass(). I've also inspected the initialization of the returned wrapped java.lang.class object, and it is the same for constructors with string argument (m_jClass = AndroidJNI.NewGlobalRef(androidJavaObject.GetRawObject())).

So it seems the use forAndroidJavaClass is when I don't want to have a java.lang.object initialized, as you suggested. It makes sense if initializing this object is expensive, and if you don't have already an AndroidJavaObject initialized.

But... An AndroidJavaObject is initialized in the constructor of AndroidJavaClass, which break your comments.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Post to VKontakte from Android app? 0 Answers

Unity to Android: passing CameraCharacteristics.Key values from Unity to Android cameraCharacteristics.get? 1 Answer

Using AndroidJavaObject properly 1 Answer

NotFoundException: File res/mipmap-xhdpi-v4/ic_launcher.png from xml type layout resource 0 Answers

Android Plugin in Unity - No Return Value 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges