본문 바로가기
자바(Java)/자바 클래스

[Java Class.10] this 키워드

by 긱펀 2024. 3. 26.
반응형

[Java Class.10] this 키워드

 

1. this 키워드

  • this 키워드는 클래스 안에서 자기 자신의 object를 가리킬 때 사용합니다.

 

[예제1] 아래 코드를 보며 this의 사용에 대해 이해해 보자.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Person {
    // instance variables
    private String name;
    private String email;
    private String phoneNumber;
 
    // constructor
    public Person(String theName)
    {
        this.name = theName;
    }
 
    // accessor methods - getters
    public String getName()
    {
        return this.name;
    }
 
    public String getEmail()
    {
        return this.email;
    }
 
    public String getPhoneNumber()
    {
        return this.phoneNumber;
    }
 
    // mutator methods - setters
    public void setName(String theName)
    {
        this.name = theName;
    }
 
    public void setEmail(String theEmail)
    {
        this.email = theEmail;
    }
 
    public void setPhoneNumber(String thePhoneNumber)
    {
        this.phoneNumber = thePhoneNumber;
    }
 
    public String toString()
    {
        return this.name + " " + this.email + " " + this.phoneNumber;
    }
}
 
public class Main {
    public static void main(String[] args) {
      Person p1 = new Person("Sana");
      System.out.println(p1);
      Person p2 = new Person("Jean");
      p2.setEmail("jean@gmail.com");
      p2.setPhoneNumber("404 899-9955");
      System.out.println(p2);
    }
}
cs

 

  • 위 코드에서 this.name, this.email, this.phoneNumber라고 코딩한 부분은 사실 name, email, phoneNumber라고 그냥 코딩해도 되지만, "this.variable"형태로 굳이 코딩한 이유는 현재 object의 instance variable을 가리킨다는 것을 확실하게 구분하기 위해서 입니다.
  • static method 안에서는 this, instance variable을 사용할 수 없습니다. 그 이유는 static method가 classname으로만 실행되고 object를 만들어서 실행되지 않기 때문입니다. this와 instance variable은 class밖에서 object가 만들어진 다음에 사용가능 합니다.
  • static method 안에서 굳이 this, instance variable을 사용하려면 static method 안에서 직접 obejct를 만들고 사용해야 합니다.

 

[예제2] this는 종종 object variable형태로 method의 argument에도 사용가능 합니다. 아래의 코드를 보세요.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class Pay
{
    private double pay;
 
    public Pay(double p)
    {
        pay = p;
    }
 
    public double getPay()
    {
        return pay;
    }
 
    public void calculatePayWithOvertime()
    {
        // this Pay object is passed to the Overtime constructor
        Overtime ot = new Overtime(this);
        pay = ot.getOvertimePay();
    }
 
    public static void main(String[] args)
    {
        Pay myPay = new Pay(100.0);
        myPay.calculatePayWithOvertime();
        System.out.println(myPay.getPay()); // 150.0
    }
}
 
class Overtime
{
    private double payWithOvertime;
 
    public Overtime(Pay p)
    {
        payWithOvertime = p.getPay() * 1.5;
    }
 
    public double getOvertimePay()
    {
        return payWithOvertime;
    }
}
cs

 

 

[유제1]

Consider the following class definitions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Pay
{
    private double pay;
 
    public Pay(double p)
    {
        pay = p;
    }
 
    public double getPay()
    {
        return pay;
    }
 
    public void calculatePayWithOvertime()
    {
        // this Pay object is passed to the Overtime constructor
        Overtime ot = new Overtime(this);
        pay = ot.getOvertimePay();
    }
}
 
public class Overtime
{
    private double payWithOvertime;
 
    public Overtime(Pay p)
    {
        payWithOvertime = p.getPay() * 1.5;
    }
 
    public double getOvertimePay()
    {
        return payWithOvertime;
    }
}
cs

 

The following code segment appears in a class other than Pay or Overtime.

1
2
3
Pay one = new Pay(20.0);
one.calculatePayWithOvertime();
System.out.println(one.getPay());
cs

 

What, if anything, is printed as a result of executing the code segment?

 

A. 10.0
B. 15.0
C. 20.0
D. 30.0
E. Nothing is printed because the code will not compile.

 

 

 


 

[유제 정답은 아래 "더보기" 클릭]

더보기

[유제1 정답]

D

(The pay starts at 20 and then increases with overtime by multiplying by 1.5.)

728x90
반응형

댓글