PowerPoint Presentation by vSTnB6


									The Java Keyword this

        Marie J. Garlitos
Instance Methods

• First we introduce instance methods:
  – any method not declared with a static keyword
  – operates on an instance of the class (an object)
    instead of operating on the class itself
  – this object instance is often referred to as the receiving
    instance of the instance method
       Instance Methods

    • A simple class Circle

     Using Instance Methods
• To use an instance method from outside of the class in
  which it is defined we must prepend a reference to the
  instance that is to be operated on:
 //create a Circle object and store in c
 Circle c = new Circle();
 c.r = 2.0; //set an instance field of object
 //invoke an instance method of the object
 double a = c.area();      Notice the method does not
                                  have a parameter. How does it
                                  know what data to operate on?
The Object Reference this

• All instance methods are implemented with
  an implicit parameter not shown in the
  method signature.
• The implicit argument is named this
• It holds a reference to the object through
  which the method is invoked. In our example,
  that object is a Circle.
3 Common Uses of this

   • accessing shadowed fields
   • invoking another overloaded constructor in
     the same class
   • passing this as a parameter
Accessing Shadowed Fields

• In some cases a field in a class can be
  shadowed or hidden inside a method by a
  parameter or a local variable by the same
• For example, when a method parameter has
  the same name as one of the fields of the
  class, you must use this to refer to the field.
Accessing Shadowed Fields
                • Looking at our previous
                  example class Circle, we
                  add the method
                  setRadius(). The local
                  variable r shadows the
                  instance field r. We must
                  use this.r to refer to the
                  instance field set to
                  argument r of the method
Accessing Shadowed Fields

• In general, avoid shadowing variables
• Shadow only instance fields with local
  variables that serve as their temporary copies
  in an instance method or constructor.
• Copy the local variables back to the instance
  fields before leaving the method
Invoking other Constructors of Same

                  • A class can define two or more
                    overloaded constructors with
                    same name and different
                    argument lists.
                  • Sometimes it is useful for one
                    overloaded constructor to invoke
                    another in the same class.
Invoking other Constructors of Same

• In our given example class calcData, the statement
  this(30); //line number 12
 is used by an overloaded constructor
  public calcData()//line no. 10; also
                  //called a noarg
                  //constructor (no parms.)
 to invoke another overloaded constructor while passing
  an integer value of 30 as a parameter.
Invoking other Constructors of Same

   • That constructor stores the value of the incoming
     parameter (30) in the instance variable named
     myData (line number 16)
   • Control is then returned to the noarg constructor (line
     10) which in turn returns control to main() method.
   • New object has been constructed and the
     instance field myData (line 02) belonging to
     that object contains the value 30.
Passing this as a Parameter

   • When one object invokes a method in another object
     and passes a reference to itself as a parameter (also
     referred to as registration).
   • The method in the second object saves the reference
     that it receives as an incoming parameter.
   • This makes it possible for a method in the second
     object to make a callback to the first object later when
Passing this as a Parameter
                  • To illustrate:
                    Department d contains a
                    method (newFaculty())
                    which invokes a method
                    belonging to Faculty a
                    passing a reference to itself
                    as a parameter (line 26).
Passing this as a Parameter

   • Faculty a saves that reference in an
      instance variable for later use enabling it to
      make a callback to Department d.
   • The main point is that the this reference
     is available to all instance methods belonging
      to an object and can be used whenever there
      is a need for reference to the object on which
      the method is invoked.
Why use this?

• To summarize:
  – To provide more information, more clarity.
  – To distinguish between fields and local/ parameters
    variables of the same
  – To make it possible for one overloaded constructor to
    invoke another overloaded constructor in the same
  – To pass a reference to the current object to a method
    belonging to a different object.
• Jia, Xiaoping. Object Oriented Software Development
   using Java- 2nd edition, Adison Wesley 2003
• Baldwin, Richard. The Essence of OOP using Java, The
   this and super Keywords. 2006.
• Flanagan, David. Java in a Nutshell 3rd edition,
  O’Reilly and Associates, Inc 1999, 1997 and 1996.

To top