0
When one has to use private modifier?
4 Answers
+ 6
Private modifier is used to introduce ENCAPSULATION in java.
Private variables scope is within the class only.
so, you can understand the importance of Private access modifier to achieve security levels in java programs and CONFIDENTIAL information can be stored in variables using Private access modifier.
+ 3
when you don't want the user to acess to that value/function, or when it's internally in the class. Atributes are always privated, and you access them by Getter and Setters
+ 2
private modifier is useless. U can modify a private variable from outside the declaring class. see my code. Private is limited to compile time. If the compiler sees u accessing a private variable outside its scope, it flashes an error. Whereas at runtime there is no concept of private,protected modifiers. At runtime a variable is just a memory location whether in stack or the data segment.
0
When you want not to give an access to user in attributes and methods on a class.