File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes how can private provide security for variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how can private provide security for variables" Watch "how can private provide security for variables" New topic

how can private provide security for variables

raghu mrvreddy

Joined: Nov 24, 2006
Posts: 7
My friend was asked a question in google about private access modifiers.

question was :

if you have setters and getters methods for accessing private variables, how are you providing security for private variables if the client can set the private variable value using setter methods

in advance
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

I didn't think I was providing security when I did that. But I might have been; it depends on what you mean by "security" there.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
There is a strong school of thought that says you shouldn't generally have setters at all, just getters, and set every field through the constructor only (essentially making as many objects immutable as possible).

You could say you're providing the option of increased security when using setters as you can write them to provide for example validation (or even authorisation), though you certainly don't have to.

Ådne Brunborg
Ranch Hand

Joined: Aug 05, 2005
Posts: 208
In a setter, it is really easy to add data verification, e.g.

Maybe, by "providing security", Raghuveer was referring to something like this?

Entia non sunt multiplicanda praeter necessitatem
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
Encapsulation is what is provided by OOP, not security. I suspect what you mean is encapsulation.

Making an object immutable is no different security-wise, or encapsulation-wise then having mutators. Whether you are passing data through a constructor gains no "security" benefits over passing it to a mutator. There are plenty of benefits to using immutable classes but are related to performance and memory usage.

"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Well, that would depend on your definition of security. If I define it as "I instantiate an object and I want to prevent you from changing it" then immutable is more secure than a setter. If I mean "I want to let you change it within some domain of values" then a setter with validation is more secure than a public variable. And if I mean "I don't want you to know this variable even exists" ... what implementation would you choose?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: how can private provide security for variables
It's not a secret anymore!