File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Getter Setter Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getter Setter Question" Watch "Getter Setter Question" New topic

Getter Setter Question

Charles Angemeyer

Joined: Dec 18, 2012
Posts: 22
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?

Greg Charles

Joined: Oct 01, 2001
Posts: 2968

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.

I agree. Here's the link:
subject: Getter Setter Question
It's not a secret anymore!