JAVA

JAVA - 멤버변수와 this

Cong_S 2022. 7. 1. 17:39
public class Member {
	
	String name;
	String tel;
	String address;
	
	void setMember(String name, String tel, String address) {
		// this를 붙이면 멤버변수를 지정할 수 있다.
		this.name = name;
		this.tel = tel;
		this.address = address;	
	}

 

멤버변수와 로컬변수를 구별할 때 this를 사용한다.

 

클래스의 멤버변수 이름은 name, tel, address

setMember라는 메소드의 파라미터 값도 name, tel, address

이기 때문에 두 개를 같이 써야할 때는 오류가 날 수 있다.

이 때 변수 앞에 this 를 붙여쓰면 해당 변수는 멤버변수로 특정하여 사용할 수 있다.