'this' 키워드의 5가지 다른 사용 방식

When programming in object-oriented languages like Java or JavaScript, you will often come across the ‘this’ keyword. The ‘this’ keyword refers to the current instance of a class or object and can be used in various ways. In this article, we will explore five different uses of the ‘this’ keyword.

1. Referencing Current Object

The primary use of the ‘this’ keyword is to reference the current object within a class or method. It allows you to access instance variables and methods of the object you are currently working with. For example:

public class Person {
   private String name;

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

Here, the ‘this’ keyword is used to refer to the current object, allowing us to assign the value of the ‘name’ parameter to the instance variable ‘name’.

2. Differentiating Local and Instance Variables

Another use of the ‘this’ keyword is to differentiate between local variables and instance variables with the same name. Consider the following example:

public class Rectangle {
   private int width;
   private int height;

   public Rectangle(int width, int height) {
      this.width = width;
      this.height = height;
   }
}

Here, the ‘this’ keyword is used to reference the instance variables, distinguishing them from the parameters passed to the constructor.

3. Invoking Another Constructor

The ‘this’ keyword can also be used to invoke another constructor within the same class. This is known as constructor chaining. For example:

public class Car {
   private String make;
   private String model;

   public Car(String make) {
      this.make = make;
   }

   public Car(String make, String model) {
      this(make); // invoking the single-argument constructor
      this.model = model;
   }
}

In this example, the second constructor invokes the first constructor using the ‘this’ keyword, passing the ‘make’ argument.

4. Returning Current Object

The ‘this’ keyword can be used to return the current object from a method, allowing method chaining. This is commonly seen in builder patterns. For example:

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

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

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

Here, the methods ‘withName’ and ‘withAge’ return the current object, allowing method chaining like ‘person.withName(“John”).withAge(25)’.

5. Passing Current Object to Another Method

Lastly, the ‘this’ keyword can be used to pass the current object as a parameter to another method. This allows you to call methods on one object within the context of another object. For example:

public class Calculator {
   public void add(int num) {
      // add implementation
   }

   public void calculateSum() {
      add(5); // calling add method on the current object
   }
}

In this case, the ‘calculateSum’ method invokes the ‘add’ method using the ‘this’ keyword, passing the current object as an implicit parameter.

In conclusion, the ‘this’ keyword has various uses in object-oriented programming, including referencing the current object, differentiating variables, invoking constructors, returning the current object, and passing the current object to other methods. Understanding these different uses will help you utilize the ‘this’ keyword effectively in your code.

#programming #objectoriented #thiskeyword