问题描述:

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?

网友答案:

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.

网友答案:

The public modifier is for normal compilation units. In this regard the no other app can call your classes. However, theoretically it should be possible, by writing your own class loader and using reflection. But at this level, the public modifier is meaningless, because with Reflection you can access all attributes, regardless of their modifier. For such an approach only a SecurityManager would help.

相关阅读:
Top