Consider the class Circle:

import java.lang.Math;

public class Circle
{
    // Accessor methods
    public double getX()      { return m_x; }
    public double getY()      { return m_y; }
    public double getRadius() { return m_r; }

    // Mutator methods
    public void setX(double v)      { m_x = v; }
    public void setY(double v)      { m_y = v; }
    public void setRadius(double r) { m_r = r; }

    // Operations/Attributes
    public double getCircumference()
                { return 2 * Math.PI * m_r; }
    public double getArea()
                { return Math.PI * m_r*m_r; }

    // Data
    double m_x, m_y; // coordinates of center
    double m_r; // radius
}

Circle has a number of members:

  • member variablesdata members, or fields
    • m_x,  m_y, and m_r
  • member functions, or methods
    • accessor methods:
      • double getX()
      • double getY()
      • double getRadius()
    • mutator methods:
      • void setX(double v)
      • void setY(double v)
      • void setRadius(double r)
    • other methods:
      • double getCircumference()
      • double getArea()

Question: Are getCircumference() and getArea() operations or accessor methods??

Instance Members

What does being an instance member mean?

Here’s a picture that will help you conceptualize things (I hope!).

We have a class defined as follows:

class Circle
{
  //...
  double m_r;
  //...
}

(we’re concentrating on the important parts, and ignoring the details)

and we create three instances of this class:

Circle c1 = new Circle();
Circle c2 = new Circle();
Circle c3 = new Circle();

Here’s what the result looks like:

That is, each instance retains its own copy of the instance variable m_r, and thus the value of m_r is specific to each instance.

Instance Methods and this

Let’s show the Circle class again:

import java.lang.Math;

public class Circle
{
    // Accessor methods
    public double getX()      { return m_x; }
    public double getY()      { return m_y; }
    public double getRadius() { return m_r; }
    // Mutator methods
    public void setX(double v)      { m_x = v; }
    public void setY(double v)      { m_y = v; }
    public void setRadius(double r) { m_r = r; }
    // Operations/Attributes
    public double getCircumference()
                { return 2 * Math.PI * m_r; }
    public double getArea()
                { return Math.PI * m_r*m_r; }

    double m_x, m_y; // coordinates of center
    double m_r; // radius
}

The above methods are actually instance methods.

This means that each method ‘knows’ which instance it is being called against.

For example, if we have code like:

Circle c1 = new Circle();
Circle c2 = new Circle();
double r1 = c1.getRadius();
double r2 = c2.getRadius();

the calls to each getRadius() method will return the size of the radius for its respective instance.

The code for getRadius() looks like:

public class Circle
{
 // ...
 public double getRadius() { return m_r; }
 // ...
}

which could have been written:

public class Circle
{
 // ...
 public double getRadius() { return this.m_r; }
 // ...
}

Essentially, the call to the method:

double r1 = c1.getRadius();

appears as if it were being called like this:

double r1 = getRadius(c1);

and as if the method were defined:

public double getRadius(Circle this) 
{ return this.m_r; }

In other words, it is as if there is a hidden parameter being implicitly passed to the function.

this:

  • is an implicitly declared variable
  • is local to each instance method
  • references the instance against which the method was invoked
  • is normally implicit;  however, there are times when you need to use this explicitly (see later).

Initializers and final Values

You may:

  • set the value of an instance variable in a constructor

or:

  • initialize it in the class 

The latter is preferred, especially if the instance variable is typically being initialized to the same value every time.

double m_radius = 0.0;
int    m_countDownStart = 10;

You may also declare an instance variable as final. Here’s an example from the Java Math class:

public final static double PI = 3.14159265358979323846;

final means that it’s the ‘final value’ — in other words, it’s a constant, and once initialized can’t be changed.

final variables must be initialized; if you forget:

static final double m_interestRate;

you’ll get an error:

'final' members must be initialized

Such final instance variables are initialized when the instance is created.

JDK Version 1.1 introduced the concept of blank finals, which allow you to not include an initializer on the declaration of a final variable, but that variable must still have a value assigned to it before it is used.  I doubt you’ll find a reason to do this, except in very unusual circumstances.