How does "this" keyword work in OOPS?

Asked 08-Mar-2018
Updated 12-Jul-2018
Viewed 548 times

1 Answer


0

‘this keyword’ 
this keyword is used to point to the current object or reference.

Usage of java this keyword:
There are following reason where this keyword is used.
1. this keyword is used to point out the current class instance variable.
2. this keyword is used for call the current class method.
3. this keyword is also invoked the current class constructor.
4. this can be passed as an argument when you call the method.
5. this keyword is used to initialization the value of any variable.
6. this keyword is used for return the current class instance from the method.

Working on this keyword in OOPS Concept:
Example:
public class Student {
 private String name;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name
 }
}
class Test {
 public static void main(String[] args) {
  Student s = new Student();
  s.setName(“ABCD”);
  System.out.println(s.getName());
 }
}