[Java] Encapsulation

What is encapsulation?

Access modifier

Field permissions

//Bad example
public class Person {
  public String name;
  public int age;

  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public String show(){
    return String.format("%s(%d years old)\n", this.name, this.age);
  }
}

Accessor Method

public class Person {
  //Field is private
  private String name;
  private int age;

  //getter in the name field
  public String getName() {
    return this.name;
  }
  //name field setter
  public void setName(String name) {
    this.name = name;
  }
  //age field getter
  public int getAge() {
    return this.age;
  }
  //age field setter
  public void setAge(int age) {
    if (age <= 0) {
      throw new IllegalArgumentException("Please specify the age as a positive number.");
    }
    this.age = age;
  }

  public String show() {
    return String.format("%s(%d).", getName(), getAge());
  }
}
public class AccessorBasic {

  public static void main(String[] args) {
    var p = new Person();
    p.setName("Ichiro");
    p.setAge(47);
    System.out.println(p.show()); //I'm Ichiro (47 years old).
    p.setAge(-30);  //error
  }
}

Immutable class

//Declare the class itself as final and do not extend it
public final class Person {
  //All private final (reassignment prohibited)
  private final String name;
  private final int age;

  //Initialize private final field in constructor
  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }
  //Getter
  public String getName() {
    return this.name;
  }

  public int getAge() {
    return this.age;
  }
}
public class ImmutableBasic {
  public static void main(String[] args) {
    var p = new Person("Satoshi", 10);
    System.out.println(p.getName()); //Satoshi
  }
}
import java.util.Date;

public final class Person {
  private final String name;
  private final int age;
  private final Date birth;

  public Person(String name, int age, Date birth) {
    this.name = name;
    this.age = age;
    this.birth = birth;
  }

  public String getName() {
    return this.name;
  }

  public int getAge() {
    return this.age;
  }
  
  public Date getBirth() {
    return this.birth;
  }
}
//Example of changing the value passed to the instance later
import java.util.Date;
public class Main {
  public static void main(String[] args) {
    var birth = new Date();
    var p = new Person("Satoshi", 10, birth);
    System.out.println(p.getBirth());  //Tue Nov 03 11:26:33 UTC 2020
    //Update the object passed during instantiation
    birth.setDate(15);
    System.out.println(p.getBirth());  //Sun Nov 15 11:26:33 UTC 2020

//Example of changing the value obtained from the instance
//    var p = new Person("Satoshi", 10, new Date());
//    System.out.println(p.getBirth());  //Tue Nov 03 11:29:05 UTC 2020
//    var birth = p.getBirth();
//
//    birth.setDate(15);
//    System.out.println(p.getBirth());  //Sun Nov 15 11:29:05 UTC 2020
  }
}   

How to prevent invariance from breaking

//Constructor argument birth,Extract the time stamp value from the birth field with the getTme method and create a new object
import java.util.Date;

public final class Person {
  private final String name;
  private final int age;
  private final Date birth;

  public Person(String name, int age, Date birth) {
    this.name = name;
    this.age = age;
    //Defensive copy
    this.birth = new Date(birth.getTime());
  }

  public String getName() {
    return this.name;
  }

  public int getAge() {
    return this.age;
  }

  public Date getBirth() {
    //Defensive copy
    return new Date(this.birth.getTime());
  }
}

Recommended Posts

java (encapsulation)
[Java] Encapsulation
What is Java Encapsulation?
Java
Encapsulation
Java
Java encapsulation private public
Java encapsulation and getters and setters
Java learning (0)
Studying Java ―― 3
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
Java inheritance
Java basics
Decompile Java
[Java] Annotation
java notes
Encapsulation review
java beginner
Java (add2)
JAVA (Map)
[java] interface
Java9 collection