• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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: 5006
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.
 
He's my best friend. Not yours. Mine. You can have this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic