Review:   accessing members in an object

  • An object's member can refer to:

    • A data field in the object         or      
    • A method in the object

  • A data field or a method in an object can be accessed using the dot operator (.):

     objectRefVar.dataField  references a data field in the object.
    
     objectRefVar.method(arguments)  invokes a method on the object

  • The dot (.) operator is also known as the object member access operator


  • Key point:

    • Members in an object are always accessed through a reference variable.

    Emphasis:

    • Members in an object are always accessed through a reference variable.

Mystery:   where is reference variable used in the instance methods ???

Consider the instance methods in the Circle class definition again:

public class Circle
{
    public double radius = 1;       /** The radius of this circle */

    public Circle() { }             /** Constructor 1 for a circle object */

    public Circle(double newRadius) /** Constructor 2 for a circle object */
    {
        radius = newRadius;  // Member radius is not accessed with a ref. var !?
    }

    public double getArea()      /** Return the area of this circle */
    {
        return 3.14159 * radius * radius;   // Again ?? 
    }

    public void setRadius(double newRadius) /** Set new radius for this circle */
    {
       radius = newRadius;   // Shouldn't it be:  refVar.radius  = newRadius ??
    }
} 

The member variable radius is not accessed using a reference variable !   How is this possible ???

The implicit (= hidden) parameter this

Here is the dirty little secret about Java's instance methods:

  • An instance method is always invoked using an object reference variable:

     objectRefVar.method(arguments)  invokes a method on the object  

  • The variable objectRefVar is passed to an instance method as:

    • An implicit (= hidden) parameter

    The name of this implicit (= hidden) parameter is:

      this  


  • Let's make the hidden parameter this explicit

    (I.e.: I will reveal where the Java compiler uses the hidden parameter)

The implicit (= hidden) reference variable this in the instance methods

When we define a class as follows:

public class Circle
{
    public double radius = 1;       /** The radius of this circle */

    public Circle() { }             /** Constructor 1 for a circle object */

    public Circle(double newRadius) /** Constructor 2 for a circle object */
    {
        radius = newRadius;
    }

    public double getArea()      /** Return the area of this circle */
    {
        return 3.14159 * radius * radius;
    }

    public void setRadius(double newRadius) /** Set new radius for this circle */
    {
       radius = newRadius;
    }
} 

The implicit (= hidden) reference variable this in the instance methods

The Java compiler will secretly insert the implicit parameter this in every instance method:

public class Circle
{
    public double radius = 1;       /** The radius of this circle */             

    public Circle(Circle this) { } 

    public Circle(Circle this, double newRadius) 
    {
        radius = newRadius;
    }

    public double getArea(Circle this)     
    {
        return 3.14159 * radius * radius;
    }

    public void setRadius(Circle this, double newRadius)             
    {
       radius = newRadius;
    }
} 

The parameter this is a copy of the reference variable in the method invocation (E.g.: circle1.getArea())

The implicit (= hidden) reference variable this used by the instance methods

The Java compiler will also insert this. to every member access in an instance method (and constructor):

public class Circle
{
    public double radius = 1;       /** The radius of this circle */

    public Circle() { }             /** Constructor 1 for a circle object */

    public Circle(double newRadius) /** Constructor 2 for a circle object */
    {
        this.radius = newRadius;
    }

    public double getArea()      /** Return the area of this circle */
    {
        return 3.14159 * this.radius * this.radius;
    }

    public void setRadius(double newRadius) /** Set new radius for this circle */
    {
       this.radius = newRadius;
    }
} 

We can add the this variable explicitly ourselves (if you wish)
DEMO: 10-classes/08-this/Circle.java      (proof that the parameter variable this exists !)

The implicit (= hidden) parameter this

Illustrated:   how the reference variable is used as the implicit parameter this and to access radius:

  • When we invoke circle1.getArea(), the Java compiler will "secretly" pass circle1 as this to getArea():

    Therefore, the call circle1.getArea() will compute:

     circle1.radius × circle1.radius × π  =  2 × 2 × π

The implicit (= hidden) parameter this

Example that illustrate the passing of the implicit parameter:

  • And when we invoke circle2.getArea(), the Java compiler will "secretly" pass circle2 as this to getArea():

    And, the call circle2.getArea() will compute:

     circle2.radius × circle2.radius × π  =  4 × 4 × π

When is the use of this necessary ???

  • The implicit (= hidden) parameter "this" is almost never necessary to write Java classes.

  • There is only 1 case that I can think of when it is necessary to use the "this" variable (which you can easily avoid):

    • When a parameter variable has the same name as an instance variable in the class

    Example:

        /** Set new radius for this circle */
        public void setRadius(double radius)
        {
           radius = radius;  // Error: radius is ambiguous !
    
           // Fix: this.radius = radius;
        }  

    You can fix this problem by using:    this.radius

DEMO: 10-classes/09-this/Circle.java     --   edit and re-compile