How do getters and setters work?

ajsie picture ajsie · Jan 10, 2010 · Viewed 520.6k times · Source

I'm from the php world. Could you explain what getters and setters are and could give you some examples?

Answer

Paul Creasey picture Paul Creasey · Jan 10, 2010

Tutorial is not really required for this. Read up on encapsulation

private String myField; //"private" means access to this is restricted

public String getMyField()
{
     //include validation, logic, logging or whatever you like here
    return this.myField;
}
public void setMyField(String value)
{
     //include more logic
     this.myField = value;
}