Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Set and get

 
David Starr
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Feel free to enhance or correct these statements: From my understanding the setter and getter are set as private and separate due to protection from being altered and ease in updating. This is done over hard-coding the values (in this case) of 70 and 8. It seems like more code to do this verses simply hard-coding. Also, if a method is termed as 'void' how come the void bark() method is returning some print statements?



===========
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

David Starr wrote:Feel free to enhance or correct these statements: From my understanding the setter and getter are set as private and separate due to protection from being altered and ease in updating.

The instance variable is private. The setter and getter are public. If you search for "encapsulation", you can read about why protection from alteration is useful. My favorite is that you can add validation.

David Starr wrote: Also, if a method is termed as 'void' how come the void bark() method is returning some print statements?

Bark isn't returning a print statement. It is calling a print statement. Just like your main method is calling setSize() and System.out.println(). Any method can call other methods. Only methods with a return type can return a value.
 
David Starr
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. It seems to make sense. The code itself came from a textbook, but I will need to understand this more. I suppose I just need to keep looking over it and getting more repetition. Thank you again.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
David Starr
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I am honored to be included.

Sincerely,
David
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic