Login Register Sign up for the GRM e-newsletter

Login to post Forums » Off-topic discussion » Java 101 questions
  • mrwillie

    Feb. 3, 2009 9:03 p.m. mrwillie New Reader

    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

    Feb. 4, 2009 10:27 a.m. Tim Baxter Online Editor

    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

    Feb. 4, 2009 10:29 a.m. Strizzo Dork

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

  • CrackMonkey

    Feb. 4, 2009 11:08 a.m. CrackMonkey Reader

    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

    Feb. 4, 2009 9:33 p.m. mrwillie New Reader

    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

    Feb. 5, 2009 9:12 a.m. CrackMonkey Reader

    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

    Feb. 5, 2009 2:18 p.m. mrwillie New Reader

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

 
Tire Rack- Revolutionizing Tire Buying

You'll need to log in to post.