๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โ˜•Java

[Java ์ดํ•ดํ•˜๊ธฐ] 17. Java์˜ this

by Luvbit 2024. 5. 27.


 

๐Ÿ’› Java์˜ this๋ž€?

Java์—์„œ 'this' ํ‚ค์›Œ๋“œ๋Š” ํ˜„์žฌ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํŠน์ˆ˜ํ•œ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. 
'this'๋Š” Instance method์˜ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•ญ์ƒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

 

 

 

๐Ÿฉท this์˜ ํŠน์ง•

 

1. ํ˜„์žฌ ๊ฐ์ฒด ์ฐธ์กฐ 

  • 'this' ํ‚ค์›Œ๋“œ๋Š” ํ˜„์žฌ ๋ฉ”์„œ๋“œ๋‚˜ ์ƒ์„ฑ์ž๊ฐ€ ์†ํ•œ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋Š” ๋ฉ”์„œ๋“œ์™€ ์ƒ์„ฑ์ž์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ์ฐธ์กฐ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค.

2. ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ๋กœ์ปฌ ๋ณ€์ˆ˜์˜ ๊ตฌ๋ณ„

  • ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ๋กœ์ปฌ ๋ณ€์ˆ˜๊ฐ€ ๋™์ผํ•œ ์ด๋ฆ„์„ ๊ฐ€์งˆ ๋•Œ,
  • this๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ๋ช…ํ™•ํžˆ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3. ํ˜„์žฌ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

  • this๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ๊ฐ์ฒด์˜ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ณดํ†ต ์ƒ๋žต ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๋ช…ํ™•์„ฑ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4. ํ˜„์žฌ ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž ํ˜ธ์ถœ

  • ์ƒ์„ฑ์ž์—์„œ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ this๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ด๋Š” ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋”ฉ์„ ํ†ตํ•ด ์ดˆ๊ธฐํ™” ๋กœ์ง์„ ์žฌ์‚ฌ์šฉํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค

5. ๋ฉ”์„œ๋“œ ์ฒด์ด๋‹

  • ๋ฉ”์„œ๋“œ๊ฐ€ this๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด, ๋ฉ”์„œ๋“œ ์ฒด์ด๋‹์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋Š” ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ์œ ์—ฐ์„ฑ์„ ๋†’์—ฌ์ค๋‹ˆ๋‹ค.

 

 


 

 

 this์˜ ์‚ฌ์šฉ๋ฒ•์„ ์˜ˆ์ œ์™€ ํ•จ๊ป˜ ๋‹ค๋ค„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์˜ˆ์ œ 1  )  ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ๋กœ์ปฌ ๋ณ€์ˆ˜์˜ ๊ตฌ๋ณ„

public class Example {
    private int value;

    public Example(int value) {
        this.value = value; // this.value๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜, value๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜
    }

    public void setValue(int value) {
        this.value = value; // this.value๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜, value๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜
    }

    public int getValue() {
        return this.value; // this๋Š” ์ƒ๋žต ๊ฐ€๋Šฅ
    }
}

 

 

 

์˜ˆ์ œ 2  )  ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ๋กœ์ปฌ ๋ณ€์ˆ˜์˜ ๊ตฌ๋ณ„

public class Example {
    public void methodA() {
        System.out.println("methodA");
    }

    public void methodB() {
        this.methodA(); // this๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ methodA ํ˜ธ์ถœ
    }
}

 

 

์˜ˆ์ œ 3  )  ํ˜„์žฌ ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž ํ˜ธ์ถœ

public class Example {
    private int value1;
    private int value2;

    public Example(int value1) {
        this(value1, 0); // this๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๋ฒˆ์งธ ์ƒ์„ฑ์ž ํ˜ธ์ถœ
    }

    public Example(int value1, int value2) {
        this.value1 = value1;
        this.value2 = value2;
    }
}

 

 

 

์˜ˆ์ œ 4  )  ๋ฉ”์„œ๋“œ๋‚˜ ์ƒ์„ฑ์ž์—์„œ ํ˜„์žฌ ๊ฐ์ฒด ๋ฐ˜ํ™˜

public class Example {
    private int value;

    public Example setValue(int value) {
        this.value = value;
        return this; // this๋ฅผ ๋ฐ˜ํ™˜
    }

    public void printValue() {
        System.out.println(this.value);
    }

    public static void main(String[] args) {
        Example example = new Example();
        example.setValue(10).printValue(); // ๋ฉ”์„œ๋“œ ์ฒด์ด๋‹
    }
}

 

 

 

์˜ˆ์ œ 5) ๋ฉ”์„œ๋“œ ์ฒด์ด๋‹

public class Person {
    private String name;
    private int age;
    private String address;

    public Person setName(String name) {
        this.name = name;
        return this;
    }

    public Person setAge(int age) {
        this.age = age;
        return this;
    }

    public Person setAddress(String address) {
        this.address = address;
        return this;
    }

    public void displayInfo() {
        System.out.println("Name: " + name + ", Age: " + age + ", Address: " + address);
    }

    public static void main(String[] args) {
        Person person = new Person();
        person.setName("pipi").setAge(5).setAddress("Apeach House").displayInfo();
    }
}

- set ๋ฉ”์„œ๋“œ์—์„œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ๋•Œ๋ฌธ์—, ํ•œ ์ค„๋กœ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค.

 

 


 

 

๐Ÿ’œ this์˜ ์žฅ์ 

1. ๋ช…ํ™•์„ฑ : 
- this๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์ฝ”๋“œ์˜ ๋ช…ํ™•์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
- ํŠนํžˆ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ๋กœ์ปฌ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์งˆ ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
- ์˜ˆ๋ฅผ ๋“ค์–ด, ์ƒ์„ฑ์ž๋‚˜ ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2. ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ฑ :
- ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋”ฉ์—์„œ this๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•จ์œผ๋กœ์จ ์ดˆ๊ธฐํ™” ๋กœ์ง์„ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
- ์ด๋Š” ์ฝ”๋“œ ์ค‘๋ณต์„ ์ค„์ด๊ณ  ์œ ์ง€๋ณด์ˆ˜๋ฅผ ์šฉ์ดํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

3. ๋ฉ”์„œ๋“œ ์ฒด์ด๋‹ :
- this๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ๋ฉ”์„œ๋“œ ์ฒด์ด๋‹์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
- ์ด๋Š” ๊ฐ์ฒด ์„ค์ •์„ ์ง๊ด€์ ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

4. ๋™์  ๋ฐ”์ธ๋”ฉ : 
- this๋Š” ๋™์  ๋ฐ”์ธ๋”ฉ(dynamic binding)์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹คํ˜•์„ฑ์„ ํ™œ์šฉํ•  ๋•Œ ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.
- ์˜ˆ๋ฅผ ๋“ค์–ด, ์ž์‹ ํด๋ž˜์Šค์—์„œ ์˜ค๋ฒ„๋ผ์ด๋“œ๋œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ, this๋Š” ์‹ค์ œ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

 

๋™์ ๋ฐ”์ธ๋”ฉ์€ ์ƒ์† ๋ถ€๋ถ„์—์„œ ๋‹ค์‹œ ์ •ํ™•ํ•˜๊ฒŒ ๋‹ค๋ฃฐ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

 

 

 

 

" ์š”์•ฝ "

  • this๋Š” ํ˜„์žฌ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  • ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ ๋กœ์ปฌ ๋ณ€์ˆ˜๋ฅผ ๊ตฌ๋ณ„ํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ํ˜„์žฌ ๊ฐ์ฒด์˜ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๋‚˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ๋ฉ”์„œ๋“œ ์ฒด์ด๋‹์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ๋ฉ”์„œ๋“œ๋‚˜ ์ƒ์„ฑ์ž์—์„œ ํ˜„์žฌ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

 

 

 

 

this ํ‚ค์›Œ๋“œ๋Š” ์ž๋ฐ” ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋งค์šฐ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•ด ๋ณด๋ฉด์„œ ์ดํ•ดํ•ด ๋ณด์„ธ์š”!

์งˆ๋ฌธ์€ ์–ธ์ œ๋“ ์ง€ ๋Œ“๊ธ€์— ๋‚จ๊ฒจ์ฃผ์„ธ์š”.
๐Ÿฉต๐Ÿฐ๐Ÿฉต