Can someone w/ java programming experience explain "setter" and "getter" methods to me? I'm taking an online class, and don't really understand what we're going over in class right now. I need another perspective on it.
Thanks in advance. Will
Can someone w/ java programming experience explain "setter" and "getter" methods to me? I'm taking an online class, and don't really understand what we're going over in class right now. I need another perspective on it.
Thanks in advance. Will
OK, let me preface this by saying I HATE Java, and what little I know I picked up working with Java programmers for awhile.
I THINK those refer to getting and setting an object, often a variable. The getter method would retrieve x, and the setter would set x.
I think.
that sounds consistent with the little java (and programming) i could put up with before i switched to rocks.
Yeah, an object has attributes and the "setter" and "getter" routines are used for accessing these attributes.
Say you have an object called "person".
person.setDOB() is used to set the date of birth. person.getDOB() is used to get the person's date of birth. pesron.getAge() is used to get the person's age. A different flavor "getter" - it's calculating a value, and not just returning the field.
The end user doesn't care how the data is stored internally, they just want to access it in a meaningful and consistent manner.
Using the "setter" and "getter" routines ensures the end user doesn't screw the data up somehow. You can protect the encapsulated fields by declaring them private. Making them public would allow direct access.
CrackMonkey wrote: Yeah, an object has attributes and the "setter" and "getter" routines are used for accessing these attributes. Say you have an object called "person". person.setDOB() is used to set the date of birth. person.getDOB() is used to get the person's date of birth. pesron.getAge() is used to get the person's age. A different flavor "getter" - it's calculating a value, and not just returning the field. The end user doesn't care how the data is stored internally, they just want to access it in a meaningful and consistent manner. Using the "setter" and "getter" routines ensures the end user doesn't screw the data up somehow. You can protect the encapsulated fields by declaring them private. Making them public would allow direct access.
And person.setDOB() gets its info from the parameters passed to it?? Like in Person X = new Person( "CrackMonkey", 28) for example??
mrwillie wrote: And person.setDOB() gets its info from the parameters passed to it?? Like in Person X = new Person( "CrackMonkey", 28) for example??
Yeah, pretty much. The constructor method may or may not take all the possible attributes. For the person example, you might require gender, name, and DOB, but not height or hair color.
To set the height or hair color, you would have to use a "setter" in a subsequent line of code. So, you could have something like (don't take my code as perfect - I don't code Java any more):
Person Jim = new Person("Jim Smith", someDate, "male"); *someDate is an instance of Date()
Jim.setHeight(68); *height in inches
Jim.setHairColor("red");
Jim.getDOB() would return an instance of date equal to the value passed into the constructor
Jim.getAge() would return an integer value for the person's age, calculated using the date of birth
Jim.getHairColor() would return "red" etc
You'll need to log in to post.