Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
How to use this keyword in java 
 

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

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

6 comments so far (
post your own) View All Comments Latest 10 Comments:

Its very nice notes to me. i understood this concept.
thank u

Posted by saravanan on Saturday, 11.29.08 @ 03:51am | #82170

Your example is really beneficial for beginners like me.

Posted by jujahar on Friday, 01.25.08 @ 14:29pm | #46114

Why to use this?

Posted by nilam shaikh on Saturday, 01.5.08 @ 16:29pm | #44567

plz could u explain me wt tdoes the below written statement mean

import java.this.*;

Posted by sana fatima on Monday, 11.12.07 @ 19:04pm | #36979

what does this statement mean
import java.this.*;

Posted by sana fatima on Monday, 11.12.07 @ 19:02pm | #36978

Thanks ... for the help . It was really precise and useful.

Posted by Avinash on Wednesday, 04.4.07 @ 23:55pm | #13356

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.