Java

this, super

hs_developer 2022. 6. 25. 16:58
public class ThisSuperTest {

	int value= 10;
	
	public ThisSuperTest(int value)
	{
		System.out.println(value);
	}
	
	public static void main(String[] args) {
		
		ThisSuperTest tst= new ThisSuperTest(1); // 1
	}
}
public class ThisSuperTest {

	int value= 10;
	
	public ThisSuperTest(int value)
	{
		System.out.println(this.value);
	}
	
	public static void main(String[] args) {
		
		ThisSuperTest tst= new ThisSuperTest(1); // 10
	}
}

 

this는 클래스 영역에서 정의한 변수와 동일한 이름의 변수가 사용되는 경우 this. 를 붙여 클래스 영역에 정의한 변수를 사용할 수 있다.

 

super는 클래스 내에 이미 동일한 메서드, 변수가 있어도 super. 을 붙여 상속 받은 클래스의 메서드, 변수를 사용할 수 있다.

 

'Java' 카테고리의 다른 글

제네릭스  (0) 2022.06.25
인터페이스, 추상클래스  (0) 2022.06.25
오버라이드, 오버로드  (0) 2022.06.25
extends, implements  (0) 2022.06.24
List, Set, Map  (0) 2022.06.24