Java this Keyword

Java this Keyword
Java this Keyword 1

There are so many Use-cases of the keyword this in java

this keyword is used to refer instance variable of current class

If we have the same name for the local variable of one function and the instace variable, this problem is called ambiguity

The meaning of ambiguity ,two meanings for one,in this example compiler cannot decide whether to use instance variables or local variables, compiler throws an error,so now this keyword is used

Syntax

this.instace_varibale = local_variable;

example program for this keyword refers instance variable



class ex {
    int x;
    int y;

   ex(int x,int y){
        this.x=x;
        this.y=y;
    }
    void show()
    {
        System.out.println("x="+x+"    "+"y="+y);
    }
    

    public static void main(String args[])
    {
        ex obj1=new ex(3,5);
        ex obj2=new ex(5,10);
        obj1.show();
        obj2.show();

    }
}
x=3    y=5
x=5    y=10


class ex {
    int x;
    int y;

   ex(int x,int y){
        x=x;
        y=y;
    }
    void show()
    {
        System.out.println("x="+x+"    "+"y="+y);
    }
    

    public static void main(String args[])
    {
        ex obj1=new ex(3,5);
        ex obj2=new ex(5,10);
        obj1.show();
        obj2.show();

    }
}
x=0    y=0
x=0    y=0

this keyword is used to invoke current class method

if there is a nested method in same class,then this keyword is used for the inner method

Syntax

this.method();

if you dont use this keyword for the method compiler automatically adds this keyword while calling the method

Example

class Method{
    void method1(){
      System.out.println("This is method 1");
    }
    void method2(){
        System.out.println("This is method 2");
      this.method1();
    }
 
    public static void main (String[] args) {
      Method m = new Method();
      m.method2();
    }
  }
This is method 2
This is method 1

this keyword is used to Invoke current class constructor

this keyword can be used for class constructor too, this( ) constructer is used to reuse the current class constructed.

Bascally this( ) is used for constructer chaining. one constructer is called from another constructer. if you didnt use the this( ) then you will have dublicate code ,becuase we are using diffrent constrcuters depedendng on number of parameters

Advantage of using this( ) constructer is you can skip dublicate code for each constructer,lets look into example

Syntax

this(para1, para2,..................., paraN);

Example

class number {

    int a,b,c;

    number(int i,int j,int k){
        this(i,j);
        c=k;
    }

    number(int i,int j){
        this(i);
        b=j;
    }
    
    number(int i){
        a=i;
    }
 
    void show(){
        System.out.println(a+"\t"+b+"\t"+c);
    }


    public static void main( String[] args ) {
        number n1=new number(10);
        number n2=new number(5,7);
        number n3=new number(34,4,12);
        n1.show();
        n2.show();
        n3.show();
    }
    
}
10      0       0
5       7       0
34      4       12

when this( ) constructer is used inside the other constructer,this keyword should be in first line of the constructor in which we are calling.

this keyword is used as an argument in the method

this keyword can be passed as a argument in the method for class, this is used to represent as a object of the same class

syntax

function(this);

example

public class xxx2 {
    int x;
    int y;
    void add(xxx2 o){
        o.x += 5;
        o.y += 10;
    }


    xxx2(int a,int b){
        x=a;
        y=b;
        System.out.println("before passing to the add( ) method");
        System.out.println("x = " + this.x + ", y = " + this.y);
        add(this);
        System.out.println("after passing to the add( ) method");
        System.out.println("x = " + this.x + ", y = " + this.y);
    }
    

    public static void main(String args[]){
        xxx2 obj=new xxx2(20,30);

    }
}
before passing to the add( ) method
x = 20, y = 30
after passing to the add( ) method
x = 25, y = 40

this keyword is used Return object of class

In the above example this keyword can be used as argumnet in method,but this keyword can also be used to return the object

return type of the method which returns object should be its class name

syntax

return this;

example

class box {

    int x,y;
    box(int a,int b){
        x=a;
        y=b;
    }
    
    box copy(){
        return this;
    }


    public static void main(String args[]){
        box b1=new box(10,20);
        box b2;
        b2=b1.copy();
        System.out.println("x: " + b1.x+ " y: " + b1.y);
        System.out.println("x: " + b2.x+ " y: " + b2.y);
    }
}
x: 10 y: 20
x: 10 y: 20

Default image
Vishal Devxo
Vishal is a DevOps engineer and a Backend developer, he spends all his time for creating good tutorials with better visuals and blogging, developed some projects based on Python-Django, some hacking modules and scripts in python
Leave a Reply