0

I'm new to programming and I have a question about getter/setter methods. I'm a little confused on how they work with each other. For instance, would something like this work?

private boolean switchOn;
// getter method for instance field
public boolean switchOn () {
    return switchOn;
// setter method for instance field
public void setOn (boolean on){
    this.switchOn = switchOn(on);
}
hf392322
  • 19
  • 1
  • 1
  • 5
  • 2
    With the given code: not at all. You'll get a compile time error as there is no `switchOn(boolean)`. – akuzminykh Nov 11 '20 at 19:05
  • Generally, no, your setter is not going to call your getter. Each is going to access/mutate the underlying state directly (that's `this.switchOn` in your case). – jarmod Nov 11 '20 at 19:06
  • They don't "work with each other", they are separate methods. One accepts a value to set the private value, one returns the private value. And their names should be consistent and reflect the value they are setting/returning. – David Nov 11 '20 at 19:08

2 Answers2

0

The purpose of getters and setters is to keep class variables private, while still being able to access them publicly.

In the definition of a class method such as your setter, you can simply access the class variable directly by name. Using the getter would be redundant.

As a side note, it is typical to call your getter and setter functions as getVarName and setVarName.

blackbrandt
  • 2,010
  • 1
  • 15
  • 32
0

Not quite. The purpose of setters and getters is to provide a way to retreive a field or set a field in a class. They should be named based on what they do (get or set). Usually the fields are declared private as the user does not need to know the implementation details of how they are stored internally. The user just needs to know what types they take or return or what action they perform.


private boolean switchSetting = false; // default to off

// later in the class define setters and getters to
// retrieve or modify the switch setting

public boolean getSwitchSetting() {
    return switchSetting;
}
public void setOn (){  // you don't need an argument. Name says it all
    this.switchSetting = true;
}
public void setOff() { // ditto
    this.switchSetting = false;
}

Note above you could do something like this for setting the switch.

boolean ON = true;
boolean OFF = false;
public void setSwitch(boolean setting) {
     this.switchSetting = setting;
}
//  and later

object.setSwitch(ON);
// or
object.setSwitch(OFF);
WJS
  • 36,363
  • 4
  • 24
  • 39
  • So basically getter methods are used to access the private instance field and the setter method can be used to modify it? Is that why you can pass a parameter through the setter method and use this.switchSetting = setting? I hope these questions make sense I really appreciate your help! – hf392322 Nov 11 '20 at 19:41
  • Yes that is what they do. Also, let's say you had an immutable class like String. You can't just get the length directly because if you could you might be able to change it. So you use a getter to allow access to a field without allowing someone to change it. In this case no setters would be declared. – WJS Nov 11 '20 at 19:44