Java
TOPIC 38 – INSTANCE METHODS
LESSON NOTE
INTRO
We have
already worked with the first two components of classes – data fields and
constructors. In this lesson, we will
look at including the third and final component to classes – instance methods.
Instance methods allow us to store functionality related to the class inside
the class.
SIMILAR TO
FUNCTIONS
Instance methods are very similar to functions. They have a method prototype at the top
that works in the exact same way. They
have a method body that is executed each time the method is called.
The one
difference is that instance methods are not static. So static doesn't appear in the
prototype. As a result, these methods
have access to the data fields of the object they are called on (more below).
DETAILS ABOUT INSTANCE
METHODS
Instance
methods are called using an object name.
The general form is:
objectName.methodName(…).
All instance
methods have access to the object's data fields. There is no need to pass the data fields to
the methods. They can simply be used
at any time.
EXAMPLE - CIRCLE CLASS
In this
example, we will add an instance method to the following circle class:
public
class Circle
{
public double radius;
public Circle(double
tmpr)
{
radius = tmpr;
}
}
The
instance method that we will add is called area(). It calculates the area of the circle
(using the radius) and return that value.
public
double area()
{
double a = Math.PI
* radius * radius;
return a;
}
The entire
class is below:
public
class Circle
{
//DATA FIELDS
public double radius;
//CONSTRUCTOR(S)
public Circle(double
r)
{
radius = r;
}
//INSTANCE
METHOD(S)
public double area()
{
double a = Math.PI
* radius * radius;
return a;
}
}
We can now
create a Circle object and try out the instance method.
public
class CircleTester
{
public static void main(String[] args)
{
//Create Circle
object c with radius 10.
Circle c = new Circle(10.0);
//Output the area of the Circle object
to screen.
System.out.println("The
area is " + c.area());
}
}
|
EXAMPLE - POINT CLASS
Let’s
consider the Point class from last topic.
We will add a method called distanceFromOrigin() that will simply calculate and return the Point’s
distance from the origin.
public double distanceFromOrigin()
{
return Math.sqrt(x * x + y * y);
}
The updated point class would look like this:
public class Point
{
//DATA FIELDS
public
double x;
public
double y;
//CONSTRUCTOR(S)
public Point(double
xvalue, double yvalue)
{
x = xvalue; //setting x data field
y = yvalue; //setting y
data field
}
public Point()
{
x = 0.0;
y = 0.0;
}
//INSTANCE METHODS
public
double distanceFromOrigin()
{
return Math.sqrt(x * x + y * y);
}
}
The
following statements would create a Point object p and output to screen p’s
distance from the origin.
Point p = new Point(12.1,
2.3);
System.out.println(“Dist.
from origin: “ + p.distanceFromOrigin());
|
SPECIAL METHOD – toString()
It is good
practice to include a toString() method in our classes.
The job of the method is to simply return a String version of the
object. It could include the values of
the all the datafields or perhaps just the key datafields.
The toString()
method is special because it is automatically called when Java needs to
convert your object into a String.
This is notably the case inside the System.out.println()
statement.
The toString()
methods for a Point class might look like this:
public String
toString()
{
return
"(" + x + " ," + y +
")";
}
Since toString()
is automatically called by Java when we need a String version of our object,
we can do the following to call the toString()
method:
System.out.println(p);
|