How To Calculate Area Of Rectangle Using This Keyword In Java ?

How to use the this keyword in Java ?.

How to use the this keyword in Java ?.

The this keyword is a special keyword frequently used in the Java programming language. Usually, this keyword is used when we want to refer currently executing object. Here this keyword is the reference to a calling object itself. When a object calls a method also called as Instance method, it has variable this which refers to object which has called it. By using this keyword you can refer or access to any field of a particular object in a method or constructor. Whenever an instance method or constructor is called this keyword

is set to a variable holding reference to a object who has called it.

package com.hubberspot.this.example;

class Rectangle{

private int length;
private int breadth;

Rectangle(int length){

/> this.length = length;

Rectangle(int length, int breadth){
this.breadth = breadth;
public int area(){
return (length*breadth);

public class ThisTest {
public static void main(String [] args){
Rectangle rect = new Rectangle(5,5);

System.out.println("The Area of rectangle is : "+
+ rect.area());



Output of the program :-

The Area of rectangle is : 25

For more of such hot topics read my other posts here :

Visit my new website

Article Written By jontymagicman

I am DInesh Varyani.

Last updated on 26-07-2016 151 0

Please login to comment on this post.
There are no comments yet.
Variables And Data Types In Java Programming Language.
How Do I Sort Array Elements Using Bubble Sort Algorithm ?