Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

private variables, public getter and setter. Why?

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mr Dittmer for the reply.
This was a very apppropriate answer i think.
Thanks a lot.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic