mrwillie
mrwillie New Reader
2/3/09 9:03 p.m.

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

Tim Baxter
Tim Baxter Online Editor
2/4/09 10:27 a.m.

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.

Strizzo
Strizzo Dork
2/4/09 10:29 a.m.

that sounds consistent with the little java (and programming) i could put up with before i switched to rocks.

CrackMonkey
CrackMonkey Reader
2/4/09 11:08 a.m.

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.

mrwillie
mrwillie New Reader
2/4/09 9:33 p.m.
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??

CrackMonkey
CrackMonkey Reader
2/5/09 9:12 a.m.
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

mrwillie
mrwillie New Reader
2/5/09 2:18 p.m.

I think I understand. It's alittle dif. when I sit down to actually write something. Thanks for the help.

You'll need to log in to post.

Our Preferred Partners
2HoE5cBDQJEMdGjkonfUYY3kIpxwUCdgiJ4h5DG5ZZuAPDJbpZPJw4x0hhixr3A2