그러게요 왜사용할까요 ?
이 질문에 답할 수가 없었다.
항상 이렇게 써야한다 ~ 이었고 프로젝트에서 Vo 패키지 내에 클래스들을 Getter & Setter로 만들었어서 그냥 정말 당연히 그렇게 코딩해야하는줄.
하지만 당연한게 어딨겠어.

Apple이라는 객체가 있다고 가정해보고, apple.color으로 프로퍼티로 바로 접근해 색 값을 가져와보겠다.
public class Apple {
private String color;
private int count;
public Apple(String color, int count {
this.color = color;
this.count = count;
}
}
위와 같이 접근하지 않고 getColor(), setColor() 메서드를 통해 경유해서 설정하도록 하는 기법이 Getter & Setter 개념이다.
public class Apple {
private String color;
private int count;
public Apple(String color, int count) {
this.color = color;
this.count = count;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public static void main(String[] args) {
Apple a1 = new Apple("red", 5);
System.out.println(a1.getColor()); // red
}
}
public class Animal {
private String species;
public String getSpecies() {
return species;
}
public void setSpecies() {
this.species = species;
}
}
public void setCount(int count) {
if(count > 0) {
this.count = count;
} else {
throw new IllegalArgumentException("개수는 0개보다 많아야 합니다.");
}
}public void setSalary(double salary) {
this.salary = salary * 0.9; // 세금 공제 로직 추가
}public void setColor(String color) {
System.out.println("변경된 색상 : " + color);
this.color = color;
}결국 객체지향 프로그래밍에서
1. 데이터를 안전하게 보호하고
2. 객체의 유연성과 유지보수성을 향상시키기 위해
3. 필요한 경우에만 사용하고
4. 과도하게 사용해 객체의 상태를 외부에서 쉽게 조작하지 않도록 주의하기 위해
사용한다는 것!