최원종의 개발 블로그

this 예약어 사용 방법 3가지 본문

Java/Java 객체지향

this 예약어 사용 방법 3가지

chl6698 2026. 2. 25. 15:58

this 사용법 3가지

  1. 자기 자신의 주소를 가리킨다
  2. 생성자에서 다른 생성자를 호출할 수 있다
  3. 자신의 주소 값을 반환시킬 수 있다.

실습 코드

Person (this를 이용해 다른 생성자 호출)

package ch10;

//클래스를 설계하는 측
public class Person {

    //this 에 3가지 사용법이 존재한다
    //1. 자기 자신의 주소를 가리킨다
    //2. 생성자에서 다른 생성자를 호출할 수 있다
    //3. 자신의 주소 값을 반환시킬 수 있다.

    private String name;
    private int age;
    private String phone;
    private String gender;

    //생성자
    //1. 사용방법
    public Person(String name, int age) {
        //컴파일러는 가까운 변수에 이름부터 확인한다
        // 매개변수 = 매개변수(x)
        //멤버변수 = 매개변수(o)
        this.name = name;
        this.age = age;
        System.out.println("1번 생성자 호출 됨");
        //this는 자기 자신의 주소를 가리킨다

    }

    //this 사용방법 2번째
    //this()는 생성자에서 다른 생성자를 호출할 때 사용할 수 있다.
    public Person(String name, int age, String phone) {
        //this.name = name;
        // this.age = age;
        this(name, age); // 다른 생성자 호출
        this.phone = phone;
        System.out.println("2번 생성자 호출 됨");
    }

    public Person(String name, int age, String phone, String gender) {
//        this.name = name;
//        this.age = age;
//        this.phone = phone;
        this(name, age, phone);
        this.gender = gender;
        System.out.println("3번 생성자 호출 됨");
    }

    //3번째 사용 방법 -> 메서드 자기 자신의 주소값을 반환시킬 수 있다
    public Person getPerson() {
        //값으로 자신의 주소값을 반환한다. 생성자 아님
        return this;
    }

    //매서드
    String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}
package ch10;

public class PersonMainTest {

    public static void main(String[] args) {

        Person person1 = new Person("홍길동", 100, "010-1234-1234", "F");

        System.out.println(person1.getName());
        System.out.println(person1.getAge());

    }//end of main
}//end of class
더보기
출력결과화면

2.UserInfo(생성자 호출 실습)

 

클래스 설계

package ch10;

public class UserInfo {
    private String userId;
    private String userPassword;
    private String userName;
    private String userAddress;
    private String phoneNumber;

    //생성자 오버로딩 5개 생성하기
    public UserInfo(String userId) {
        this.userId = userId;
        System.out.println("1번 생성자 호출");
    }

    public UserInfo(String userId, String userPassword) {
        this(userId);
        this.userPassword = userPassword;
        System.out.println("2번 생성자 호출");
    }

    public UserInfo(String userId, String userPassword, String userName) {
        this(userId, userPassword);
        this.userName = userName;
        System.out.println("3번 생성자 호출");
    }

    public UserInfo(String userId, String userPassword, String userName, String userAddress) {
        this(userId, userPassword, userName);
        this.userAddress = userAddress;
        System.out.println("4번 생성자 호출");
    }

    public UserInfo(String userId, String userPassword, String userName, String userAddress, String phoneNumber) {
        this(userId, userPassword, userName, userAddress);
        this.phoneNumber = phoneNumber;
        System.out.println("5번 생성자 호출");
    }
}

 

실행하는 코드

package ch10;

public class UserInfoMainTest {
    public static void main(String[] args) {
        UserInfo userInfo1 = new UserInfo("chl6698@naver.com","12341234","최원종","20202373","010-1234-1234");

    }
}

'Java > Java 객체지향' 카테고리의 다른 글

static키워드 이해  (0) 2026.02.26
get, set 메서드  (0) 2026.02.26
접근 제어 지시자 (access modifier)  (0) 2026.02.25
객체지향 프로그래밍(객체간 상호작용)  (1) 2026.02.25
생성자  (0) 2026.02.24