this keyword sample in Java

By: Reema Sen Emailed: 1696 times Printed: 2202 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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.

For example, the Point class was written like this

public class Point {
    public int x = 0;
    public int y = 0;
	
    //constructor
    public Point(int a, int b) {
	x = a;
	y = b;
    }
}

but it could have been written like this:

public class Point {
    public int x = 0;
    public int y = 0;
	
    //constructor
    public Point(int x, int y) {
	this.x = x;
	this.y = y;
    }
}

Each argument to the second 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.

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. Here's another Rectangle class, with a different implementation from the one in the Objects section.

public class Rectangle {
    private int x, y;
    private int 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;
    }
    ...
}

This class contains a set of constructors. Each constructor initializes some or all of the rectangle's member variables. The constructors provide a default value for any member variable whose initial value is not provided by an argument. For example, the no-argument constructor calls the four-argument constructor with four 0 values and the two-argument constructor calls the four-argument constructor with two 0 values. As before, the compiler determines which constructor to call, based on the number and the type of arguments.

If present, the invocation of another constructor must be the first line in the constructor.


Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Reema Sen
this keyword sample in Java
Using the DriverManager Class vs Using a DataSource Object for a connection
Steps to get a Free SSL certificate for your Tomcat
fgets(), fputs() - Line Input and Output - sample program in C
Address Arithmetic and pointers in C
Constants and escape sequences in C
Implementing Pure Virtual Functions in C++
strcat() and strncat() sample program in C++
paint() sample program to draw a line in J2ME
Creating and Handling JAR files
SequenceInputStream example program in Java
FilenameFilter - sample program in Java
Transient vs Volatile modifiers in Java
while (1) Loops in C++
Use of 'finally' in Java

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
Student marks calculation program in Java
XML and Java - Parsing XML using Java Tutorial
append() in Java
How to use ArrayList in Java
Garbage collection and Finalize() method
Using StringTokenizer in Java
Abstract classes in Java
Increment and Decrement Operator
Read from a COM port using Java program
Integer: byte, short, int, and long data types in Java
indexOf( ) and lastIndexOf( ) in Java
Count number of vowels, consonants and digits in a String in Java
Using toLowerCase( ) and toUpperCase( ) in Java
The Basic Structure of a Simple Java program
Most Emailed Articles (in Java)
How to use ArrayList in Java
Write to a COM port using Java program
Read from a COM port using Java program
How to access instance from an inner class and accessing outer class variables in java
Data Types in Java
Tomcat and httpd configured in port 8080 and 80
Save/Write/Read image file from/to a database using Java program
Java program for changeable wrapper class
History of Java
Method Overloading (function overloading) in Java
Sample Java program shows how to write to COM port using Java.
Sample program to demonstrate the use of ActionListener
Introducing JavaScript.
Assigning Object Reference Variables
Bitwise Operator Assignments