this가 하는 일

행복한 콩🌳·2023년 2월 20일
0

JAVA

목록 보기
13/26
  • 자기 자신의 메모리를 가리킴

  • 생성자에서 다른 생성자를 호출

  • 자신의 주소를 반환 함

  • 인스턴스 마다 다른 값을 가진다.

  • 생성된 인스턴스 스스로를 가리키는 예약어


BirthDay라는 클래스에서 day라는 인스턴스를 만들고
day 안에 setYear라는 메소드를 실행시킴
day는 참조변수

  • 객체 내에서 생성자에서 다른 생성자를 호출할 때 사용
  • this는 가장 먼저 나와야한다. this 앞에는 어떤 statement도 올 수 없음
package thisex;
class Person{
    String name;
    int age;

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

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

    public Person returnSelf() {
        return this;
    }
}
public class CallAnotherConst {
    public static void main(String[] args){
        Person p1 = new Person();
        System.out.println(p1.name);

        System.out.println(p1.returnSelf());
    }
}
profile
매일매일 조금씩 모여 숲이 되자🐣

0개의 댓글