JVM/Java, are method accessibility rules enforced at runtime?

前端 未结 4 943
北恋
北恋 2021-01-06 15:44

I was curious about how the JVM works. Does the JVM acknowledge method accesibility rules like \'private\' protected or is that only done at compile time?

For examp

4条回答
  •  死守一世寂寞
    2021-01-06 16:25

    Oli mentioned it rightly that ultimately you can do anything if you come to extent of byte code manipulation (if done correctly !!!). Although I will like answer your question of accessibility honor at runtime in Java. If you have any doubts then please go ahead and use reflection to call the private method of one class from other class and you will get your answer. Java creates the function table of class at runtime when loading it and allow the refererence to the functions in limit of accessibility rule. However Java provides facility where you can call the private methods via reflection using setAccessible(true) on the method reference before invoking it.

提交回复
热议问题