Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change accessibility in Runtime

Tags:

java

As mentioned here:

"accessibility is a static property that can be determined at compile time; it depends only on types and declaration modifiers."

In this case, what does java.lang.reflect.Field.setAccessible(boolean) function really do in runtime?

like image 930
MBZ Avatar asked Apr 22 '26 13:04

MBZ


1 Answers

setAccessible does not alter the accessibility level of a member. It only allows a certain member to be accessed through reflection alone. This doesn't explicitly break the rules of accessibility, since your code doesn't really access the member: it invokes a Reflection method that internally does some magic, accessing the member on your behalf.

With this in mind there is also a crucial point to note: Java also has a very powerful and unpenetrable security system that can deny the usage of reflection to foreign code. Whenever needed, this mechanism can be used to truly prevent access to private members.

like image 102
Marko Topolnik Avatar answered Apr 25 '26 01:04

Marko Topolnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!