(자바) 이

그만큼

여기에는 생성된 인스턴스의 메모리 주소가 포함됩니다.

public void setYear(int year) {
        this.year = year // this.year = 인스턴스의 변수, year = 매개 값
}
public static void main(String args()) {
        BirthDay day = new BirthDay();
        day.setYear(2000);
}

위의 코드가 실행되면 main 함수는 args그리고 day스택 공간을 차지 BirthDay 개체의 인스턴스 day힙 메모리에 로드됩니다.

다음으로 setYear() 메서드가 스택으로 이동합니다. setYear 메서드의 this.year는 힙에 로드되는 BirthDay 인스턴스의 멤버 변수를 가리킵니다.

생성자가 다른 생성자를 호출하는 경우

클래스에 여러 생성자가 있는 경우 this를 사용하여 생성자 내에서 다른 생성자를 호출할 수 있습니다.

다음 코드와 같이 호출된 모든 메서드가 완료되면 즉, 한 생성자가 다른 생성자를 호출하면 인스턴스가 생성됩니다. this() 문 앞에 다른 문을 사용할 수 없습니다.

public class Person {
    String name;
    int age;

    public Person(){
        this.name = "이름없음";

                // Call to 'this()' must be first statement in constructor body
        this("이름없음", 1); 
    }

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

스스로 돌아올 때

public class Person {
    String name;
    int age;

    public Person(){
        this("이름없음", 1);
    }

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

    public Person getPerson(){
        return this;
    }

    public static void main(String() args) {
        Person person1 = new Person();
        Person person2 = new Person();

        System.out.println(person1.getPerson());
        System.out.println(person2.getPerson());
    }
}

객체와 인스턴스 주소를 반환합니다.

Person@776ec8df
Person@4eec7777