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