Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to restrict the variable not to access outside the class in java?

Tags:

java

I am using java. I know by using private access modifier we can restrict. But using Reflection API still I can access the variable outside the class. Then what is the use of private modifier here?

like image 909
user1016403 Avatar asked Dec 23 '11 17:12

user1016403


1 Answers

private prevents you accessing it from other classes using Java. However using JNI or a library you can do things differently. You can prevent reflection with a security manager but this is rarely needed.

Note: some libraries like Serialization need to be able to access private fields to work.

like image 173
Peter Lawrey Avatar answered Nov 02 '22 23:11

Peter Lawrey