• Nem Talált Eredményt

USING this REFERENCE 6.3

In document Programming Languages (Pldal 159-164)

The object that receives a message are sometimes called receiver object because the object receives the message. For example, let us look at the statement below:

John.calculateFee( );

In this statement, message calculateFee() will be received by the object John, that is the current execution object. This means that John is the receiver object of the sent message. When an object receives a message, it will respond by executing the proper method.

In the member method code of a class, sometimes we have to refer to the receiver object. In Java, this receiver object can be called using this keyword.

Basically this keyword is used for the following situations:

First Situation: Differentiate attribute and local variable or parameter in a member method or constructor (if they declared with the same name)

Second Situation: Pass the current execution object as a parameter to another method

Third Situation: To call another constructor in the same class.

The following program demonstrates the first and second situations:

//first constructor

Line 5: public Date(int day, int month, int year) { Line 6: this.day=day;

Line 7: this.month=month; this.day, this.month and this.year are referring to the object attributesÊ while day, month and year are referring to the parameters in the method

Line 8: this.year=year;

Line 9: }

//second constructor

Line 10: public Date(Date dte) {

this.day, this.month and this.year are referring to the object attributesÊ while dte.day, dte.month and dte.year are referring to the attributes of the Date object pass through the parameter in the method

Line 11: this.day=dte.day;

Line 12: this.month=dte.month;

Line 13: this.year=dte.year;

Line 14: }

In the first constructor of Program 6.6, the attributes and parameters are having the same name. We can differentiate the attributes and parameters by including this keyword in front the attributes. In Line 6- Line 8 and Line 11 ă Line 13, this.day, this.month and this.year are referring to the object attributesÊ while day, month and year inside the first constructor are referring to the parameters of the constructor. If this keyword has been omitted in the above program, then the compiler will treat all the „variables‰ in the first constructor as parameter. Now let us continue with program that will show the second situation in which the this keyword is been used:

The new Date object is constructed from the current object using the above second constructor. this keyword is actually will refer to the current execution object to be known during execution

Notice that this method return Date object. Besides primitive data types such as int or double, a method can also return an object.

//member method 1

We can use the following program to execute the above class:

Program 6.7:

The output of the above program is:

Date: 27/7/1974

Now let us look into Line 3 and Line 4 in the Program 6.7.

In Line 3 the object todayDate of the class Date will be created with the following state:

todayDate day =20

month = 7 year = 1974

In Line 4, the current execution object is todayDate as it will call the method addDays(7). Thus when this method is invoked by the todayDate, the keyword this that is available in Line 16 in Program 6.6 will be replaced with todayDate object (since this is the current execution object):

  Refers to the current execution

object that is object todayDate.

During runtime, the compiler automatically will replace this keyword with the current execution object

Line 16 (of Program 6.6.) will be create new dte object of the class Date using the state of the object of todayDate. In Line 17 (of Program 6.6.), the attribute day of the dte object will be added with 7. In Line 18 (of Program 6.6), this dte object will be returned by the method which eventually will be received by the object nextWeekDate in Line 4 (Of Program 6.7). Thus, the execution of the Line 4 (Of Program 6.7) will have the following affect in the memory:

day = 20

Line 5 (Of Program 6.7) will display the value of the attributes belong to the object nextWeekDate.

Thus, by using this keyword we are letting the compiler to replace the keyword this with the current execution object. If you donÊt want to use this keyword in Program 6.6, you need to explicitly state the name of the object in the appropriate method. For our case in the Program 6.6, the method addDays should be changed like this:

public Date addDays(int days, Date DATE) Date dte = new Date (DATE);

dte.day=date.day+days;

return dte;

}

Consequently, the Line 4 in the Program 6.7 should be changed like this to reflect the changes made in the addDays method of Program 6.6:

       Date nextWeekDate=todayDate.addDays(7, todayDate);

Now let us see the third situation in which the this keyword can be used. Inside a constructor, you can also use the this keyword to call another constructor in the same class as shown in the following example:

Line 1: public class Rectangle { Line 2: private int x, y;

Line 3: private int width, height;

Line 4: public Rectangle() { //first constructor Line 5: this(0, 0, 0, 0);

Line 6: }

Line 7: public Rectangle(int width, int height) { //second //constructor Line 8: this(0, 0, width, height);

Line 9: }

Line 10: public Rectangle(int x, int y, int width, int height){

//third constructor

Line 11: this.x = x;

Line 12: this.y = y;

Line 13: this.width = width;

Line 14: this.height = height;

Line 15: }

... ...

Line n: } Source:

http://java.sun.com/docs/books/tutorial/java/javaOO/thiskey.html

The above program has three constructors. The statement in Line 5 calls the second constructor while the statement in Line 8 calls the third constructor. 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. Using this keyword to call a constructor as shown in the above program is known as explicit constructor invocation.

this keyword cannot be used for static members. Thus the following program is wrong because this keyword has been used in front of the static attribute:

public class Test { static int a=8;

In object-oriented programming, usually the object will be referred to more than once. One way for you to be able to refer to an object several times in your program is by storing the object reference in a variable known as reference type variable. This variable can be used as a reference to the object. The declaration syntax for reference type variables is the same as declaring normal variables. But, instead of using data type to declare, a reference type variable must use refer to the class name as shown below:

   Student John; //Here John is reference type variable

The declaration a b o v e declares the reference t y p e variable named John whose value will refer to the object declared in Student. The above declaration also has several other consequences:

 When reference type variable is declared, it refers to no object and cannot be used.

 Objects can be created only by using new operator

In document Programming Languages (Pldal 159-164)