开发者

What names should getter and setter methods have

开发者 https://www.devze.com 2023-03-31 17:15 出处:网络
I am still very confused about getter and setter methods. I had this code; public class MethodsInstances {

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开发者_Go百科 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


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)


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());
    }
}


You can use girlName here you really don't have to call getName(). The reason you need getName() is if you you want to get the name outside of this class. For example if you create a new class and then create the above class object in the new class and assign that object a name (value for girlName) you won't be able to access girlName from new class since it is private .. so you need a public method which will get the value for you.

Also it doesn't have to be getName or setName but this just makes it easy to understand what function is doing.


It's a common design patter to encapsulate the process of "getting" and "setting" variables in methods. This gives you more freedom if you ever want to change the underlying implementation.

For an example, lets say you one day want to change the parameter girlName to be an object Girl instead; If you directly access girlName from your outer classes, you will have to change all your external code.

With a setter method, you could simply change one method and do

public void setGirlname(String name)
{
   girlname = new Girl(name, some_other_data);
}

Or perhaps you want to make sure girlname always is returned with uppercase.

public String getGirlname()
{
    return girlName.toUpperCase();
}

Thus giving you a loot more flexibility in your code design.


You must first read about abstraction, encapsulation and OOP to understand about accessors, mutators, immutability and data access.


We want to prevent direct access to the variable, we make the variable a private variable. When the variable is private, other classes are not able to access that variable. If we create variable as public it is accessible for all.

to change the actual private variable we will now need public getter() or setter(). The basic naming conventions say that we will take the name of the variable and prefix it with get and/or set.

in your specific case the getGirlname would be correct.

We call this encapsulation


This way you can inspect classes and invoke them at runtime using Reflection. See more here

HTH

Ivo Stoykov

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号