Feb 02 2009

Java: Getters and Setters

Published by Michael Doyle at 10:40 under Java

  • Let you get and set instance variables
  • Getter: sends back (as a return value) the value of what the getter is getting
  • Setter: takes an argument value and uses it to set the value of an instance variable
  1.  
  2.  
  3. public class ElectricGuitar {
  4. String brand;
  5. int numOfPickups;
  6. boolean rockStarUsesIt;
  7.  
  8. String getBrand() {
  9. return brand;
  10. }
  11. void setBrand(String aBrand) {
  12. brand = aBrand;
  13. }
  14. int getNumOfPickups() {
  15. return numOfPickups;
  16. }
  17. void setNumOfPickups (int num) {
  18. numOfPickups = num;
  19. }
  20. boolean getRockStarUsesIt() {
  21. return rockStarUsesIt;
  22. }
  23. void setRockStarUsesIt(boolean yesOrNo) {
  24. rockStarUsesIt = yesOrNo;
  25. }
  26. }
  27.  

No responses yet

Trackback URI | Comments RSS

Leave a Reply