• Post Reply Bookmark Topic Watch Topic
  • New Topic

private variables, public getter and setter. Why?  RSS feed

 
Nitin Nigam
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a small query.
Why in our class we are advised to make our variables private and provide public getters and setters for them?
If it is because we want to ensure that nobody can corrupt the values of variables by directly modifying them? But then, one can do it by calling public setter method for that property.
Or we are actually supposed to write some validation code inside setter methods, in order to make this approach some sense?

Thanks in Advance
Nitin
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Validation is one reason. Keeping the field name out of the public API also allows you to change it later without breaking the API. And it allows you to change the class later in other ways as well, e.g. moving the field to some other class (so that the public setter would call a setter in a different class). Having the setter called also allows you to do other things, e.g. notify interested other components of the change of value. None of this would be possible if the field was accessed directly.
 
Nitin Nigam
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mr Dittmer for the reply.
This was a very apppropriate answer i think.
Thanks a lot.
 
Vladimir Nesov
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you don't write anything in getter/setter at the moment, having them enables you to do so later without changeing code that uses those properties.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you read up on JavaBeans or read a lot of homework type examples you can get the impression that you should add get and set methods for all fields.

Good OO design often goes another way, toward private fields with no getters and setters. You may ask what an object can do if all its data is so private? Look to the methods to actually DO the work of the program. If you find yourself getting a couple fields from an object, doing something with them and maybe putting the results back into the object, all that work probably should have been in the object itself. Move enough work into the object and it can keep its data secret.

That little rant may have been totally off topic for what you're doing now. But if it sounded interesting, scroll on down to the OO, UML, etc forum and follow up on it.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you find yourself getting a couple fields from an object, doing something with them and maybe putting the results back into the object, all that work probably should have been in the object itself. Move enough work into the object and it can keep its data secret.

here's the same from the famous Why getter and setter methods are evil article:
Don't ask for the information you need to do the work; ask the object that has the information to do the work for you
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!