This Keyword
The this keyword in Java returns a reference to the object on which the method being executed is invoked. We can use the returned reference to access the variables and methods of the object. Look at the following example where the this keyword is used in a constructor

class Student

String name;

public Student ( String name )
this.name = name;

As we know, a constructor is used to create the object of the class. So, the keyword this returns a reference to the object being created. Therefore, this.name refers to the instance variable ‘name’ which is assigned with the ‘name’ passed to the constructor. We could have of course written the constructor as we would have normally written in the following way:

public Student ( String n ) name = n;

But, in this case, a person who is reading the code wouldn’t know what n is unless he continues to the next line. So, the use of this keyword is justified in the constructors of a class.

The this keyword is also used to access instance variables that have been hidden by local variables. We have read that a variable can be declared only once. However, there is an exception to this. We can declare an instance variable and a local variable (for example, a variable in a method) with the same name. When we use the variable name in the method body, we get the value which the local variable points at. In effect, the local variable has hidden the instance variable.

public class Student

{

int marks = 34;

public void newMarks () {
int marks = 100;
System.out.println ( marks );
}
}

The method newMarks() in the above program would print the integer 100 and not the integer 34. This is because the local variable marks has hidden the instance variable marks. In order the access the hidden variable, we make use of the this keyword in the following way.

System.out.println ( “Old marks: ” + this. Marks ); // prints 34

The this keyword has returned a reference to the object and therefore marks accessed through the this keyword refers to the marks that was declared as an instance variable.

The this keyword is also used to call one constructor from another. This is quite helpful in reducing code work when a number of instance variables are declared in a class and there is a necessity to provide a number of constructors which would be able to create objects of that class whether all the necessary parameters have been passed or not. In simpler terms, creation of the object should be made more flexible. The user should be allowed to go ahead, whatever be the information he possesses, be it only the name and rollNumber, just the rollNumber or all the details. Look at the following example of the Student class.

class Student

{

String name;
int rollNumber;
int marks1, marks2, marks3;

public Student ( String name, String rollNumber, int marks1, int marks2, int marks3 ) {
this.name = name;
this. rollNumber = rollNumber;
this.marks1= marks1;
this.marks2 = marks2;
this.marks3= marks3;
}

public Student ( String name, int rollNumber ) {
this ( name, rollNumber, -1,-1, -1 );
}

public Student ( String rollNumber ) {
this ( null, rollNumber );
}
}

When we create an object of the Student class in the following way, the second constructor would be invoked which would then invoke the first constructor.

Student s= new Student( “Sai”, 34);

And, if we use the following statement, all the three constructors would be called in effect. We are actually making call to the third constructor, which then calls the second which in turn calls the first.

Student s= new Student (34);

An important thing to be remembered when calling other constructors using the keyword this, is that such a call should be the first statement in the constructor. The following code, for instance would result in an error:

public Student ()
System.out.println (“No argument constructor called”); // incorrect
this(-1);

Leave a Reply

Your email address will not be published. Required fields are marked *