• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what are setters and getters for

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Setters and getters are used to provide access to a private variable. Could someone please explain why not just access the variable directly like sampleObject.attribute = "Value" or String attributeValue = sampleObject.attribute? What are the pitfalls of this direct access approach
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of things (at least)

1) encapsulation. The thing you are setting/getting may be complex - eg. You might create a getter that formats a Date object into a String for example.

2) By following a convention they enable tools to use reflection to manipulate them without having to know field names before hand. BeanUtils gives some examples.
 
Ranch Hand
Posts: 349
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Cruz,

More on getter setters could help you.

Ananth Chellathurai
 
Ronwaldo Cruz
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ananth,

I've actually read the article but it's anti-"setter getter". What I'm trying to do is to solicit pro-"setter getter" opinions.

Hi Daniel,

Example #1: These cases are in my opinion quite rare and in any case does not necessitate having setters and getters for all the other fields

Example #2: I think these tools built on the fact that there are setters and getters so they adjusted to the situation they found. If this is not the case, I do believe they still would have found to work their way around
reply
    Bookmark Topic Watch Topic
  • New Topic