Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What names should getter and setter methods have

Tags:

java

methods

I am still very confused about getter and setter methods. I had this code;

public class MethodsInstances {

    public MethodsInstances(String name){
        girlName = name;
    }

    private String girlName;

    public String getName(){
        return girlName;
    }

    public void sayName(){
        System.out.printf("Your first gf was %s", getName());

    }
}

But for "sayName", why couldnt you instead of using getName(), just type girlName? It would be the same, because getName() returns girlName, as seen in the code. Also, do the methods have to start with get and set, or can be named whatever you want?

Huge thanks from the newbie coder, Dan B

like image 430
Daniel Bezden Avatar asked Dec 10 '22 06:12

Daniel Bezden


2 Answers

The point of getters and setters is that only they are meant to be used to access the private varialble, which they are getting or setting. This way you provide encapsulation and it will be much easier to refactor or modify your code later.

Imagine you use girlName instead of its getter. Then if you want to add something like a default (say the default name is 'Guest' if it wasn't set before), then you'll have to modify both the getter and the sayName function.

There is no requirement for getters and setter to start with get and set - they are just normal member functions. However it's a convention to do that. (especially if you use Java Beans)

like image 172
Petar Ivanov Avatar answered Dec 22 '22 00:12

Petar Ivanov


You absolutely can use the variable directly in your example, mostly because sayName() is in the same class.

Other than that, I see 3 reasons for having getters and setters:

1.) It's a principle of object oriented programming to keep values (state) private and provide public methods for interaction with other classes.

2.) Classes with getters and setters often follow the Java beans design pattern. This pattern allows those objects to be used in template engines or expression languages like JSP or Spring.

3.) In some cases it prevents actual errors. An example:

public class DateHolder() {
    public Date date;

    public static void main(String... args) {
        DateHolder holder = new DateHolder();
        holder.date = new Date();
        System.out.println("date in holder: "+holder.date);
        Date outsideDateRef = holder.date;
        outsideDateRef.setTime(1l);
        //will be different, although we did not change anything in the holder object.
        System.out.println("date in holder: "+holder.date);
    }
}

wrapping the date variable with getter and setter that operate only with the value, not the reference, would prevent this:

public class DateHolder() {
    private Date date;
    public Date getDate() {
       return (Date)this.date.clone();
    }
    public void setDate(Date date) {
       this.date = (Date) date.clone();
    }

    public static void main(String... args) {
        DateHolder holder = new DateHolder();
        holder.setDate( new Date() );
        System.out.println("date in holder: "+holder.getDate());
        Date outsideDateRef = holder.getDate();
        outsideDateRef.setTime(1l);
        //Date in holder will not have changed
        System.out.println("date in holder: "+holder.getDate());
    }
}
like image 37
rompetroll Avatar answered Dec 22 '22 00:12

rompetroll