Java Reference
In-Depth Information
Student
StudentBody
firstName : String
lastName : String
homeAddress : Address
schoolAddress : Address
+ main (args : String[]) : void
+ toString() : String
Address
streetAddress : String
city : String
state : String
zipCode : long
+ toString() : String
FIGURE 7.2 A UML class diagram showing aggregation
or class. Inside that method, the this reference can be used to refer to the cur-
rently executing object.
For example, in a class called ChessPiece there could be a method called move ,
which could contain the following line:
if ( this .position == piece2.position)
result = false ;
In this situation, the this reference is being used to clarify which position is being
referenced. The this reference refers to the object through which the method was
invoked. So when the following line is used to invoke the method, the this refer-
ence refers to bishop1 :
bishop1.move();
However, when a different object is used to invoke the method, the this reference
refers to that object. Therefore, when the following invocation is used, the this
reference in the move method refers to bishop2 :
bishop2.move();
Search WWH ::




Custom Search