본문 바로가기

컴퓨터/JAVA

this

this란 클래스 내에서 클래스가 가지고 있는 멤버필드 또는 멤버 메소드를 직접 참조할 수 있는 자신의 참조 변수


this에 대한 정의

n 클래스 내에서 클래스가 가지고 있는 변수 또는 메소드를 직접 참조할 수 있다.

n 클래스 내에서 자신의 멤버들을 이용할 수 있는 것은 당연한 일입니다.

n 자신을 참조하는 객체 변수 this를 이용하여 멤버를 이용할 수 있습니다.

n 디자인타임에 자기 자신을 직접 참조할 수 있는 객체 변수입니다.

n 디자인타임에 자기 자신을 참조할 수 있는 유일한 키워드입니다. 


this가 혼자서 이용되면 자신의 참조값을 의미합니다.


this()는 클래스 자신의 생성자메서드를 호출할 때도 사용합니다

public class ThisSelf{

    private String name;

    private int age;

    public ThisSelf(){

        this("이름없음");

    }

    public ThisSelf(String name){

        this(name, -1);

    }

    public ThisSelf(String name, int age){

        this.name = name;

        this.age = age;

        System.out.println("name:" + name + " number:" + age);

    }

    public static void main(String[] args){

        ThisSelf ts1 = new ThisSelf();

        ThisSelf ts2 = new ThisSelf("홍길동");

        ThisSelf ts3 = new ThisSelf("김삿갓", 50);

    }

}


name:이름없음 number:-1

name:홍길동 number:-1

name:김삿갓 number:50

'컴퓨터 > JAVA' 카테고리의 다른 글

Jsoup로 파싱하기  (0) 2013.09.05
강제로 예외 발생시키기  (0) 2013.08.30
ArrayList  (0) 2013.07.05
super  (0) 2013.07.05
클래스  (0) 2013.07.05