• 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

get and set??

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do each of these get and set commands do? I've seen them in a study book, but they fail to mention what they do and are for..
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's what is called encapsulation, an OO feature.

Imagine this:
without encapsulation


Above code has a drawback. You can access the variable grandMasAge from other class without calling checkGrandMasAge for checking the age.



Therefore we need whats called encapsulation getters/setters:



Above code has major benefit: you must set the age through the setGrandMasAge and get the value by calling getGrandMasAge. No way to directly calling the variable grandMasAge.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are no get and set commands in the Java language, so I guess you mean something different. Could you please elaborate? Are you asking about getter and setter *methods*?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think what he/she means is that the accessor and mutator methods..accessor methods, in your case..the get methods, allows you to get the data while the set methods, the mutator methods allows you to change the data
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Getters and Setters are all about encapsulating data. In Java, we make instance variables private, which makes the data only directly accessible to the class, and we make the data accessible through public setter and getter method.

As we say, in Java, it's not polite to expose our privates.
 
Happily living in the valley of the dried frogs with a few tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic