WebReference.com - Part 1 of chapter 5 from Beginning Java 2 SDK 1.4 Edition, Wrox Press Ltd (7/8) | WebReference

WebReference.com - Part 1 of chapter 5 from Beginning Java 2 SDK 1.4 Edition, Wrox Press Ltd (7/8)

To page 1To page 2To page 3To page 4To page 5To page 6current pageTo page 8
[previous] [next]

Beginning Java 2 SDK 1.4 Edition

Defining Class Methods

You define a class method by adding the keyword static to its definition. For example, the class Sphere could have a class method to return the value stored in the static variable, count:

class Sphere {
  // Class definition as before...
  // Static method to report the number of objects created
  static int getCount() {
    return count;                           // Return current object count
  }
}

This method needs to be a class method because we want to be able to get at the count of the number of objects even when it is zero. You can amend the Sphere.java file to include the definition of getCount().

Note that you cannot directly refer to any of the instance variables in the class within a static method. This is because your static method may be executed when no objects of the class have been created, and therefore no instance variables exist.

Accessing Class Data Members in a Method

An instance method can access any of the data members of the class, just by using the appropriate name. Let's extend the class Sphere a little further by adding a method to calculate the volume of a Sphere object:

class Sphere {
  static final double PI = 3.14; // Class variable that has a fixed value
  static int count = 0;          // Class variable to count objects
  // Instance variables
  double radius;                 // Radius of a sphere
  double xCenter;                // 3D coordinates
  double yCenter;                // of the center
  double zCenter;                // of a sphere
  // Static method to report the number of objects created
  static int getCount(){
    return count;                // Return current object count
  }
  // Instance method to calculate volume
  double volume() {
    return 4.0/3.0*PI*radius*radius*radius;
  }
  // Plus the rest of the class definition...
}

You can see that the method volume() is an instance method because it is not declared as static. It has no parameters but it does return a value of type double--the required volume. The method uses the class variable PI and the instance variable radius in the volume calculation--this is the expression 4.0/3.0*PI*radius*radius*radius (4/3)Pir3 in the return statement. The value that results from this expression will be returned to the point where the method is called for a Sphere object.

We know that each object of the class will have its own separate set of instance variables, so how is an instance variable for a particular object selected in a method? How does our volume() method pick up the radius for a particular Sphere object?

The Variable this

Every instance method has a variable with the name, this, which refers to the current object for which the method is being called. The compiler uses this implicitly when your method refers to an instance variable of the class. For example, when the method volume() refers to the instance variable radius, the compiler will insert the this object reference, so that the reference will be equivalent to this.radius. The return statement in the definition of the volume() method is actually:

return 4.0/3.0*PI*this.radius*this.radius*this.radius;

In general, every reference to an instance variable is in reality prefixed with this. You could put it in yourself, but there's no need, the compiler does it for you. In fact, it is not good practice to clutter up your code with this unnecessarily. However, there are occasions where you have to include it, as we shall see.

When you execute a statement such as:

double ballVolume = ball.volume();

where ball is an object of the class Sphere, the variable this in the method volume() will refer to the object ball, so the instance variable radius for this particular object will be used in the calculation.

We mentioned earlier that only one copy of each instance method for a class exists in memory, even though there may be many different objects. You can see that the variable this allows the same instance method to work for different class objects. Each time an instance method is called, the this variable is set to reference the particular class object to which it is being applied. The code in the method will then relate to the specific data members of the object referred to by this.

We have seen that there are four different potential sources of data available to you when you write the code for a method:

The names of variables that are declared within a method are local to the method. You can use a name for a local variable or a parameter in a method that is the same as that of a class data member. If you find it necessary to do this then you must use the name this when you refer to the data member of the class from within the method. The variable name by itself will always refer to the variable that is local to the method, not the instance variable.

For example, let us suppose we wanted to add a method to change the radius of a Sphere object to a new radius value that is passed as an argument. We could code this as:

void changeRadius(double radius) {
  // Change the instance variable to the argument value
  this.radius = radius; 
}

In the body of the changeRadius() method, this.radius refers to the instance variable, and radius by itself refers to the parameter. There is no confusion in the duplication of names here. It is clear that we are receiving a radius value as a parameter and storing it in the radius variable for the class object.


To page 1To page 2To page 3To page 4To page 5To page 6current pageTo page 8
[previous] [next]

Created: June 24, 2002
Revised: June 24, 2002


URL: https://webreference.com/programming/java/beginning/chap5/1/7.html