Skip to main content Skip to docs navigation
TechSpiderTutorials

Java 'this' Keyword

Java 'this' Keyword

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

Using this with a Field

The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.

'this' with a Field Example

public class Point
{
  private int x=0;
  private int y=0;

  public Point(int a,int b)
  {
    x=a;
     y=b;
  }
   
 public void printPoint()
  {
        System.out.println("X value is..."+x);
        System.out.println("Y value is..."+y);
  }
 
 public static void main(String args[])
  {
	Point pt=new Point(100,200);
	pt.printPoint();
  }
}

Output:

X value is...100
Y value is...200

but it could have been written like this:

public class Point
{
  private int x=0;
  private int y=0;

  public Point(int x,int b)
  {
    x=x; //shadowing
     y=b;
  }
   
 public void printPoint()
  {
        System.out.println("X value is..."+x);
        System.out.println("Y value is..."+y);
  }
 
 public static void main(String args[])
  {
	Point pt=new Point(100,200);
	pt.printPoint();
  }
}

Output:

X value is...0
Y value is...200
					

to avoid the shadowing, it could have been written like this:

Each argument to the constructor shadows one of the object's fields inside the constructor x is a local copy of the constructor's first argument. To refer to the Point field x, the constructor must use this.x.

public class Point
{
  private int x=0;
  private int y=0;

  public Point(int x,int y)
  {
    this.x=x;
     this.y=y;
  }
   
 public void printPoint()
  {
        System.out.println("X value is..."+x);
        System.out.println("Y value is..."+y);
  }
 
 public static void main(String args[])
  {
	Point pt=new Point(100,200);
	pt.printPoint();
  }
}

Output:

X value is...100
Y value is...200

Using this with a Constructor

From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation.

'this' with a Constructor Example

class Rectangle
{
	int x,y,width,height;
	
	public Rectangle()
	{
		this(0,0,0,0);
	}
	public Rectangle(int width,int height)
	{
		this(0,0,width,height);
	}
	public Rectangle(int x,int y,int width,int height)
	{
		this.x=x;
		this.y=y;
		this.width=width;
		this.height=height;  
	}
		
	
	public void printRectangle()
	{
		System.out.println("x value is..."+x);
		System.out.println("y value is..."+y);
		System.out.println("width value is..."+width);
		System.out.println("height value is..."+height);
		System.out.println();
	}
}
public class ThisContructorDemo 
{
 public static void main(String[] args) 
 {
	 Rectangle r1=new Rectangle();
	 r1.printRectangle();
	 
	 Rectangle r2=new Rectangle(300,00);
	 r2.printRectangle();
	 
	 Rectangle r3=new Rectangle(100,100,300,400);
	 r3.printRectangle(); 
  }
}

Output:

x value is...0
y value is...0
width value is...0
height value is...0

x value is...0
y value is...0
width value is...300
height value is...0

x value is...100
y value is...100
width value is...300
height value is...400