Can a native method call a private method?

前端 未结 2 827
眼角桃花
眼角桃花 2020-12-17 18:03

I knew that in JAVA \"native\" is a special thing. It can do a lot of things. But I\'m not able to read it right now. I don\'t know how to... I knew it can call an other mat

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 18:45

    You can call private methods on a Java object that's passed to a native method via the JNI interface. It's not the same thing as within Java, calling methods on other Java objects. You have to be very careful because JNI does not enforce class, field, and method access control restrictions that are expressed through the use of modifiers such as private and final. So it can be dangerous. For example, native code can modify a final constant field of a class, after the JIT compiler has inlined it.

    Here's the relevant section of the JNI docs concerning functions and pointers: http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/design.html#wp16696

提交回复
热议问题