본문 바로가기

JAVA

[ JAVA / 자바 ] 멤버 변수와 지역 변수, this 사용법

• 멤버 변수와 지역 변수란 ?

 

* 멤버 변수 ( member variable ) 클래스의 메소드 밖에서 선언된 변수

* 지역 변수 ( local variable) 클래스의 메소드 안에 선언된 변수

 

 

 멤버 변수 this란 ?

 

- 변수명이 같을 경우, 멤버 변수와 지역 변수를 구분하기 위해 쓰는것

 

 

• 실습 예제

 

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;
		
	}
	
}