Can a private method call a private method?

I knew that in JAVA "native" is a special thing. He can do a lot. But I can’t read it right now. I don’t know how ... I knew that this could cause another mathematician in JAVA. My question is: can it call a private method? if it is YES, then only in the same class or in other classes? if it can cause another, is it a problem that could be dangerous? that is, he violated the rules. Where can I learn more about NATIVE? can anyone show me the link?

+6
source share
2 answers

The manual and specification of JNI programmers says this in "10.9 Violating Access Control Rules":

"JNI does not apply restrictions on access to the class, fields and methods that can be expressed at the level of the Java programming language using modifiers such as private and final. You can write your own code to access or change the field of an object, even if it is done at the level The Java programming language will IllegalAccessException . JNI validity was a conscious design decision, given that native code can access and change any memory location on the heap anyway. "

So, the answers to your questions:

Can it call a private method?

Yes.

if it is YES, then only in the same class or any other classes?

Any class.

if he can cause others, then is this a problem that could be dangerous? that is, he violated the rules.

The rationale for designers in order not to try to provide normal Java access rules is clearly stated in the text above. Yes, this is potentially dangerous, but any use of the JNI is potentially dangerous.

+16
source

You can call private methods on a Java object that is passed to its own method through the JNI interface. This is not the same as in Java, calling methods on other Java objects. You have to be very careful, because JNI does not apply restrictions on access to the class, fields and methods that are expressed using modifiers such as private and final . So it can be dangerous. For example, native code may modify the final constant field for a class after the JIT compiler has entered it.

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

+3
source

Source: https://habr.com/ru/post/924233/


All Articles