객체 자신의 대한 참조값을 가진다.
메소드 내에서만 사용된다.
객체 자신을 메소드에 전달하거나 리턴하기 위해 사용된다.
객체 생성자 내에서 사용할 경우, 다른 생성자를 호출한다.
매개 변수와 객체 자신이 가지고 있는 변수의 이름이 같은 경우 이를 구분하기 위해 자신의 변수에 this를 사용한다.
static메소드에서는 사용할 수 없다.
public static void main(String[] args) {
BirthDay day = new BirthDay();
day.setYear(2000);
}
public void setYear(int year) {
this.year = year;
}
public class Person {
String name;
int age;
public Person() {
this("no name", 1);
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void showPerson() {
System.out.println(name + ", " + age);
}
public static void main(String[] args) {
Person person = new Person();
person.showPerson();
}
}
public class Person {
String name;
int age;
public Person() {
this("no name", 1);
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void showPerson() {
System.out.println(name + ", " + age);
}
public Person getPerson() {
return this;
}
public static void main(String[] args) {
Person person = new Person();
person.showPerson();
System.out.println(person);
Person person2 = person.getPerson();
System.out.println(person2);
}
}
https://jaynamm.tistory.com/entry/JAVA-this-%EC%9D%98%EB%AF%B8%EC%99%80-%EC%82%AC%EC%9A%A9%EB%B2%95
https://velog.io/@imhyejeong/Java-this-%ED%82%A4%EC%9B%8C%EB%93%9C