[JAVA] About getter setter

Introduction

I'm currently learning Java and wanted to write an article about inheritance, but while I was writing I realized that I couldn't explain the getter setter, so I hurriedly changed the subject and decided to write this article.

Significance of existence and its definition

When encapsulating, if all fields are made private, reading and writing from the outside will not be possible at all. That's where the getter setter comes in. In other words, private fields that shouldn't be accessible can now be accessed via getter setters. Also, it is easy to change when you want to make it readable from the outside, but you have trouble rewriting the contents, or when you want to change the field name for some reason in the future (for example, from name to names). There are also merits such as.

** ● Getter method standard ** public field type get field name () { return this. field name; }

** ● Setter method standard ** public void set field name (field type arbitrary variable name) { this. field name = arbitrary variable name; }

Let's take a look while writing specific code for each

Let's use "getter"

//Human class
public class Human {
  void eat (Noodles r) {
    System.out.println (r.name + "Eat");
  }
}

//Noodles class
public class Noodles {
  private String name = "ramen"; 
  //name field is private by others(Here Human)Invisible to the class
}

In the above example, the following compilation error occurs.

result
name is privately accessed in Noodles

Meaning of error statement ... name cannot be accessed externally by private

So we'll add a getName method to the Noodles class to allow Humans to know the name of Noodles, in other words the value of the name field.

//Human class
public class Human {
  void eat (Noodles r) {
    System.out.println (r.getName() + "Eat");  //Modification place--①
  }
}

//Noodles class
public class Noodles {
  private String name = "ramen"; 
  
  public String getName() {                      //Modification place
    return this.name;                            //--②
  }
}

What the above getter method does is call getter from the outside (Human here) method (-①) and return the value of the name field of the Noodles class as the return value (-②). ing.

Let's use "setter"

//Human class
public class Human {
  void eat (Noodles r) {
    r.setName("Udon");                          //--③
  }
}

//Noodles class
public class Noodles {
  private String name = "ramen"; 
  
  //Added setter method
  public void setName(String name) {            
    this.name = name;
  }
}

What the above setter method does is to define the setter method in the Noodles class and call it from the outside (-③) to assign the value as an argument (rewrite the value from ramen to udon). ing.

Summary

I've written getter setter articles so far, but in reality it's ideal not to have these two. Especially for setter, it is better not to use it because the value can be changed by adding the trouble of calling the method. As for getters, I'm just referring to them, but it's better not to use them. It seems. So what if you want to know the value in private? The question arises, but I wish I could write an article about it later. Since I am currently studying, I think there are some mistakes, but I would appreciate it if you could tell me at that time.

Recommended Posts

About getter setter
java setter getter error resolution
About =
Lombok's @Getter @Setter operation memo
Automatic generation of constructor, getter / setter
About returning a reference in a Java Getter
About Kotlin
About attr_accessor
About Hinemos
About params
About Rails 6
About form_for
About Spring ③
About enum
About polymorphism
About Optional
About hashes
About JitPack
About Dockerfile
About this ()
About devise
About encapsulation
About Docker
About JAVA_HOME
Getter, Setter Inverse Problem-10 [C # Refactoring Sample]
About active_hash
About static
About exceptions
About scope
[Maven] About Maven