0

When one has to use private modifier?

25th Dec 2016, 12:24 PM
Red Heart
Red Heart - avatar
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.
27th Dec 2016, 6:06 PM
Jenny Lance
Jenny Lance - avatar
+ 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
25th Dec 2016, 12:46 PM
Nahuel
Nahuel - avatar
+ 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.
29th Dec 2016, 12:55 PM
Suvaditya
Suvaditya - avatar
0
When you want not to give an access to user in attributes and methods on a class.
2nd Jan 2017, 10:24 AM
Suzan Mahmud
Suzan Mahmud - avatar