Home Java Beginners How to use this keyword in java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

How to use this keyword in java

Advertisement
In this section, you will learn how to use the this keyword in java.

How to use "this" keyword in java

     

The keyword this is useful when you need to refer to instance of the class from its method. The keyword helps us to avoid name conflicts. As we can see in the program that we have declare the name of instance variable and local variables same. Now to avoid the confliction between them we use this keyword. Here, this section provides you an example with the complete code of the program for the illustration of how to what is this keyword and how to use it.

In the example, this.length and this.breadth refers to the instance variable length and breadth while length and breadth refers to the arguments passed in the method. We have made a program over this. After going through it you can better understand.

Here is the code of the program:

class Rectangle{
  int length,breadth;
  void show(int length,int breadth){
  this.length=length;
  this.breadth=breadth;
  }
  int calculate(){
  return(length*breadth);
  }
}
public class UseOfThisOperator{
  public static void main(String[] args){
  Rectangle rectangle=new Rectangle();
  rectangle.show(5,6);
  int area = rectangle.calculate();
  System.out.println("The area of a Rectangle is  :  " + area);
  }
}

Output of the program is given below:

C:\java>java UseOfThisOperator
The area of a Rectangle is : 30

Download this program

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 7, 2007

Related Tutorials

Ask Questions?    Discuss: How to use this keyword in java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Etta
September 24, 2011
MPkwbwznmWcCuvsQs

I can aledray tell that's gonna be super helpful.
anuj sharma
January 24, 2013
please help me to solve my problem

if we have 3 classes which are inherited then al 3 have int x; you have to print all the values of x defined in every class using overriding and this keyword. these output function is in last class where all print statements are given..... public class usethis { int x=10; } class b extends usethis { int x=20; } class c extends b { int x=30; void show() { //this.x=x; System.out.println(x); //System.out.println(x); System.out.println(this.x); } public static void main(String [] args) { c a1=new c(); a1.show(); } }
ranjit patil
January 26, 2012
this keyword

thanks sir
sameer
April 7, 2012
thanks alot

thanks alot for the explanation
lingaswamy
June 27, 2012
Java

good example
mayur
September 4, 2012
core java

explan the tostring method,
saurabh chuahan
September 12, 2012
java

i want to know about java in advance
khemchand gautam
September 28, 2012
this key word

how to use with this key word,
Anu
June 8, 2013
java

Thanks, It is written in very easy language. so really very helpful to learn concept of this keyword of java for beginners. Thanks..
gobinath
August 10, 2013
java

i need basic topics in java
DMCA.com