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

前端 未结 4 932
北恋
北恋 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:15

    If you want to call this method from outside current class you could call private & protected methods using reflection.

    Method m = RunX.class.getDeclaredMethod("test3");
    m.setAccesible(true);
    m.invoke(u);
    

    however you can call this protected (and also private) method directly from main() without any issues.

提交回复
热议问题