Why you need setters / getters in the first place

You use setter / getter, right? ..

Why do you need setters / getters? ・ What is a setter / getter? ・ Dislikes of setters / getters ・ Why you need it ・ Why does it exist? I will explain in the order of.

What is a Setter / Getter

What is a setter / getter?

.....

The one to set </ b>

Get it </ b>

This level of understanding is okay w

Example: Set / get and output yesterday, today and tomorrow dates



Date date = new Date();

private Date yesterdayDate; //Yesterday date
private Date currentDate;   //Today's date
private Date tomorrowDate;  //Tomorrow date

//setter: today's date
public void setCurrentDate(Date currentDay) {
    this.currentDay = currentDay;
}

//getter: today's date
public Date getCurrentDate() {
    return currentDay;
}

//Omitted below...
//setter: yesterday's date
//getter: yesterday's date
//setter: tomorrow's date
//getter: tomorrow's date

public static void main(String[] args) throws Exception {
        //Set today
        calendar.setTime(date);
        setCurrentDate(calendar.getTime());

        //Set yesterday
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        setYesterdayDate(calendar.getTime());

        //Set tomorrow
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        setTomorrowDate(calendar.getTime());
        
        //Show yesterday
        System.out.println("Yesterday's date:" + getYesterdayDate());
        //Show today
        System.out.println("Today's date:" + getCurrentDate());
        //Show tomorrow
        System.out.println("Tomorrow's date:" + getTomorrowDate());
}

Like this.

It's setXXXXX, getXXXXX.

In the sample, variables are set in the class and called, so the method is also private and there is no problem.

Set variables privately </ b> and setters and getters are created in public </ b>.

Disgusting parts of setter / getter

1. That alone adds code </ b> setter: 3 lines getter: 3 lines

Yes, this is just 6 lines. 6 lines even though I don't want to mess up if possible ...

2. Troublesome management </ b> For the same reason as in 1, the longer the code, the more cumbersome it is to manage. Only set and get, but two methods will be added. That's it.

3. The movement is done internally </ b>

setCurrentDate(calendar.getTime());

The method itself does not set today's date. In the method

this.currentDay = currentDay;

This is doing.

getter

return currentDay;

You can get it to return the date.


In other words, today's date itself

Pass variables with = </ b> Return variable with return </ b>

It is a translation set in. Then. ..

Why you need it

Pass variables with = </ b> Return variable with return </ b>

If you're doing it in, you shouldn't need a setter / getter. All dates

//set
this.currentDay = currentDay;
//get
currentDay;

This should be fine. .. So why do you need it?

1. To protect variables </ b>

It is public </ b> that creates and sets / gets variables in private </ b>. So, other than that, you can't set / get </ b>.

・ Prohibit setting with = above and set only with setter ・ Prohibit direct acquisition and get only with getter Limits setting / getting the value of a variable by doing so.

2. To clarify where you are setting / getting </ b>

For the same reason as 1. By using setter / getter, the place where you are setting / getting becomes clear. Also, if it falls at the place where you are getting, it becomes clear that you have not set it before that.

However, the setter / getter unnecessary theory may or may not be excited on a regular basis.

Then. ..

Why it exists

setXXX getXXX

Without looking at the internal structure This alone knows what you are doing </ b>.

setCurrentDate // Set today's date getYesterdayDate // Get yesterday's date setOneMonthAgo // Set the date one month ago getOneYearDate // Get the date one year later

Like this.

Summary

I wrote it so far, but I personally think that there are some parts that exist by convention </ b>.

Anyway, I really hate the increase in code. ..

But! Something like kotlin includes setters / getters.

val currentDate :Date; //Today's date

Yes. This is the end w

Let's say that we have setters / getters as the internal structure of var. .. By the way, at the time of val, only getters are made.

Recently, I don't have to bother to write setters / getters. Thank you ~ Thank you!

reference

https://qiita.com/katolisa/items/6cfd1a2a87058678d646

http://d.hatena.ne.jp/Nagise/20141010/1412930502

https://qiita.com/SYABU555/items/7cc5909c119ec9ab0288