Home >>Java Tutorial >This keyword in Java
this keyword in java is basically a reference of a variable that refers to the current object. There are numerous possible uses of the java this keyword.
As said earlier there are numerous use of the ‘this’ keyword in the Java language. Here are some of the important usages of java this keyword as depicted below:
1. In this example we are using ‘this’ keyword in order to refer current class instance variables:
//Java code for using 'this' keyword to //refer current class instance variables class Main { int x; int y; // Parameterized constructor Main(int x, int y) { this.x = x; this.y = y; } void display() { //Displaying value of variables a and b System.out.println("x = " +x + "y = " + y); } public static void main(String[] args) { Main object = new Main(10, 20); object.display(); } }
2. In this example we are using this() to invoke current class constructor in Java:
// Java code for using this() to // invoke current class constructor class Main { int x; int y; //Default constructor Main() { this(10, 20); System.out.println("Inside default constructor \n"); } //Parameterized constructor Main(int x, int y) { this.x = x; this.y = y; System.out.println("Inside parameterized constructor"); } public static void main(String[] args) { Main object = new Main(); } }
3. In this example we are using ‘this’ keyword in order to return the current class instance:
//Java code for using 'this' keyword //to return the current class instance class Main { int x; int y; //Default constructor Main() { x = 100; y = 200; } //Method that returns current class instance Main get() { return this; } //Displaying value of variables a and b void display() { System.out.println("x = " + x + " y = " + y); } public static void main(String[] args) { Main object = new Main(); object.get().display(); } }
4. In this example we are using ‘this’ keyword as a method parameter:
// Java code for using 'this' // keyword as method parameter class Main { int x; int y; // Default constructor Main() { x = 10; y = 20; } // Method that receives 'this' keyword as parameter void display(Main obj) { System.out.println("x = " + x + "y = " + y); } // Method that returns current class instance void get() { display(this); } public static void main(String[] args) { Main object = new Main(); object.get(); } }
5. In this example we are using ‘this’ keyword in order to invoke current class method in Java:
// Java code for using this to invoke current // class method class Main { void display() { // calling function show() this.show(); System.out.println("Inside display function"); } void show() { System.out.println("Inside show funcion"); } public static void main(String args[]) { Main t1 = new Main(); t1.display(); } }
6. In this example we are using ‘this’ keyword as an argument in the constructor call in Java:
// Java code for using this as an argument in constructor // call // Class with object of Class B as its data member class A { B obj; // Parameterized constructor with object of B // as a parameter A(B obj) { this.obj = obj; // calling display method of class B obj.display(); } } class B { int x = 5; // Default Contructor that create a object of A // with passing this as an argument in the // constructor B() { A obj = new A(this); } // method to show value of x void display() { System.out.println("Value of x in Class B : " + x); } public static void main(String[] args) { B obj = new B(); } }