Develop/Java+Kotlin
[java] this와 this()
연로그
2021. 1. 26. 15:43
반응형
this
: 인스턴스 자신을 가리키는 참조 변수
- 객체의 주소를 가리킨다
this()
: 클래스 내부의 생성자를 호출
- 생성자에서만 호출 가능
- 제일 첫 문장에서 호출 가능
- 생성자 자기 자신 호출 불가 (재귀호출 불가)
예제
public class User {
private Integer id;
protected String account;
public String password;
public User(Integer id, String account, String password) {
this.id = id;
this.account = account;
this.password = password;
}
public User(Integer id) {
this(id, "a", "b");
}
}
참조
velog.io/@damiano1027/Java-this-%ED%82%A4%EC%9B%8C%EB%93%9C
lowelllll.github.io/java/2018/12/09/java-this-%EC%B0%A8%EC%9D%B4%EC%A0%90/
반응형