//재사용 클래스
public class Student {
//인스턴스변수 ==> 데이터 저장
private String name;
private int age;
private String address;
//생성자 ==> 인스턴스 초기화
public Student() {
}
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
//getter 메서드, setter 메서드
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Student라는 인스턴스변수를 StudentChange라는 메서드를 통해 Student s라는 매개변수만으로 작업이 처리가 가능하다
public class TestStudent {
public static void studentChange(Student s) { // 인스턴스 변수와 관계 없이 매개변수만으로 작업이 가능하다
s.setAge(30);
}
public static void main(String[] args) {
Student s = new Student("홍길동",20,"서울");
System.out.println("변경전 나이:"+ s.getAge());//20
studentChange(s);
System.out.println("변경후 나이:"+ s.getAge());//30
}
}
객체에서 20을 저장을 하였으나 StudentChange라는 메서드를 통해 20이라는 값이 30으로 덮어지면서 값의 영향이 생긴다.