What names should getter and setter methods have

I am still very confused about the 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 couldn't you use getName (), just enter the name of the girl? This would be the same, because getName () returns girlName, as seen from the code. Also, should the methods begin with get and set, or can they be called as you want?

Many thanks to the new coder Dan B

+4
source share
7 answers

The point of getters and seters is that they are the only ones designed to access the private varialble variable that they get or set. This way you provide encapsulation, and it will be much easier to reorganize or change your code later.

Imagine using girlName instead of your recipient. Then, if you want to add something like a default value (say the default name is "Guest" if it has not been set before), you will have to change the getter and sayName .

For getters and setters, there is no need to start with get and set - they are just normal member functions. However, this agreement is for this. (especially if you use Java Beans )

+4
source

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

In addition, I see 3 reasons for having getters and setters:

1.) This is the principle of object-oriented programming for preserving private (private) and providing public methods for interacting with other classes.

2.) Classes with getters and setters often follow the Java beans design pattern. This template allows you to use these objects in template engines or expression languages ​​such as JSP or Spring.

3.) In some cases, this prevents actual errors. 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); } } 

Transferring a date variable using a getter and setter, which only work with a value, not a reference, will 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()); } } 
+3
source

This is a general template for encapsulating the process of "getting" and "setting" variables in methods. This gives you more freedom if you want to change the underlying implementation.

As an example, let's say that you someday want to change the girlName parameter as a Girl object instead; If you directly access girlName from external classes, you will have to change all the external code.

With the setter method, you can simply change one method and do

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

Or maybe you want to make sure that the girl’s name always comes back in capital letters.

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

Thus, you get more flexibility in code development.

+2
source

You can use girlName here, you really don't need to call getName (). The reason you need getName () is because 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 a new class and assign this object a name (the value for the name of the girl), you will not be able to access girlName from the new class, since it is private. need a public method that gets value for you.

It also doesn't have to be getName or setName, but it just makes it easier to understand what the function does.

+2
source

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

+2
source

We want to prevent direct access to the variable, we make the variable a private variable. When a variable is private, other classes cannot access this variable. If we create a variable as public, it is accessible to everyone.

to change a valid private variable, we now need a public getter () or setter (). Basic naming conventions say that we will use the variable name and prefix it with get and / or set.

in your particular case getGirlname will be correct.

We call it encapsulation

+1
source

This way you can check classes and call them at runtime with Reflection. More here

NTN

Ivo Stoykov

0
source

Source: https://habr.com/ru/post/1369266/


All Articles