Non-static vs. Static Methods

Whether or not a method is static refers to whether or not the method needs to be called with respect to a particular instance of the object.

Non-static Methods

Non-static methods are called with respect to a particular instance of an object. For instance, say you are calling an accessor, like the Dot object's getX() method. Which instance of the dot object's x-coordinate do you want? There could be hundreds of instances, all with different x-coordinates. Therefore, you cannot call getX() without knowing which instance of the Dot object you are calling it on.

Non-static Distance Method

In Lab 4, you implemented the distance between two Point objects in a non-static way:

public double distance ( Point otherPoint )
{
	// Add your code here
}

The implementation you used to implement this method was to take the coordinates from this instance (the instance this was called on) and from otherPoint and to use them in your equation. Calling your non-static distance method looked something like this:

point1.distance(point2);

One instance of Point was used to call the distance method, and another instance of Point was the parameter to the method. These were the two points used in your calculations.

Static Methods

Static methods, on the other hand, do not rely on a specific instance of an object when they are called. They are called with respect to the entire class instead of with respect to a particular instance.

An example of static methods are Math.sqrt(double a) and Math.pow(double a, double b). You do not need to create an instance of a Math object in order to use these functions. You can call them from the Math class.

Static Distance Method

For this lab, we will make a distance method that is static instead. It will look like this:

public static double distance ( Dot dot1, Point dot2 )
{
	// Add your code here
}

You will implement this method using the coordinates from dot1 and from dot2 in your equation. Calling your static distance method will looked something like this:

Dot.distance( dot1, dot2)

Two instances of the Dot class are parameters to the method. These were the two points used in your calculations. Since it is not required to call this method from any specific instance of Dot, it is static and can be called from the Dot class.

Should I Use a Static or Non-static Method?

Many methods could potentially be implemented as either static or non-static. It is up to you to consider whether or not you would like your method to be reliant on a particular instance of the object, or if it should behave more like a library function that does not need to be called from any particular instance of the object.