Java/Java

[Java] this

기록하는_사람 2022. 10. 13. 18:35

this

📌 this

① 자기 자신의 메모리를 가리킴.

	...
	
	// 생성자
	public Student(int id, String name, int birth, String phoneNumber) {  
		this.id = id;
		this.name = name;
		this.birth = birth;
		this.phoneNumber = phoneNumber;
	}
	
	...

② 생성자에서 다른 생성자를 호출함.

	...
    
	public Student() {  // 디폴트 생성자. 
		this(-1, "없음");
	}
	
	public Student(int id, String name) {  // 생성자. 
		this.id = id;
		this.name = name;
	}
    
	...

③ 자기 자신의 주소를 반환함.