Search code examples
javaandroidpublic-method

Can other apps on Android phone access public methods


According to a manual on basic Android programming, "the public modifier means that any Java class, even classes outside of the package, can use that method or variable."

I understand this concept in general terms, but am wondering how it plays out on Android platform. In theory, is it possible that an app created by another developer could access a public method in my code if they could guess how to properly call it?


Solution

  • No, because your code would never compile.

    When you call a method from another package's class, you must import that class to do so. The entire class, and all of its dependencies, must be part of your project for you to be able to compile the code successfully.

    As you do not have access to the other developer's source code, you cannot import his class. And if you can import his class, then your app will be his code as a library project or something, and hence the code will ship with your app, making your question irrelevant.