Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getter Setter Question

 
Charles Angemeyer
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working through one of those "CodingBat" exercises and I got it working.
(It is supposed to print every nth character).
My question is how do I set it up using Getters and Setters?
Did I do too much in my Setter?
Thanks.




 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a matter of style really. By convention, setters take one parameter and assign that parameter to a member variable. They may also do validation ... range checking and so forth ... but they don't usually do anything else. You could follow that convention by calling the setter something else, like outputEditedString(String, int). In that case, you wouldn't need the member variables. There's no sense setting them unless you're going to use them later. On the other hand, you could have member variables for the original string and the n, and then have getters and setters for those, or just set them via a constructor. Your outputEditedString() in that case wouldn't need parameters, since it would use the member variables instead. In neither case do you need the String result in the member data. You aren't using it, and you use result locally in your method to mean something else, so that's confusing.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic