GET, SET AND TOSTRING METHODS


CONTENTS

1. Introduction
2. Set methods
3. Get methods
 
4. To string methods
4.1 The default toString method
5. Example



1. INTRODUCTION

There are a number of Kinds of method whose function is so commonly found that they have been classified as either:

On this WWW page we will consider each of these with reference to the Circle class defined previously.




2. SET METHODS

Fields in classes are usually private (in the spirit of information hiding) and therefore cannot be accessed from outside the class. To assign values to such methods we need public methods that can be called by application classes external to the class in which they are defined.

The practice of assigning a value to a private field is so common that methods which are specifically intended to do so are called set methods. For example we could include such a method in our Circle class defined previously:

public void setCircleRadius(double cRad) {
        circleRadius = cRad;
        }

Remember that information hiding (or data encapsulation) are important programming concepts. If you look in the Java API you will find very few classes that have public fields.




3. GET METHODS

Another common requirement is for an application calls to access (get) the value associated with a private field defined in another class. To do this it is common practice to define a public method for each field which may need to be accessed that returns the value for each field. Such methods are generally referred to as "get methods". For example, again with reference to theCircle used previously, we might included:

public double getCircleRadius() {
    return(circleRadius);
    }

public double getCircleCircum() {
    return(circleCircum);
    }

public double getCircleArea() {
    return(circleArea);
    }



4. TO STRING METHODS

In the original version of the Circle class we had a method of the form:

public void outputCircAndArea() {
    System.out.println("Circumference = " + circleCircum);
    System.out.println("Area          = " + circleArea);
    }

Instead of the above it is common practice to define toString methods that return some textual representation of particular fields which can then be used in application classes for whatever purpose. Thus:

public String toString() {
    return("Circumference = " + circleCircum + "\nArea          = " + circleArea + "\n");
    }

Such "toString" methods are another common kind of method. To use the above we can embed the toString in a print statement as follows:

System.out.print(newCircle.toSting());

where newCircle is an instance of the class Circle. The above can be abreviated to:

System.out.print(newCircle);

This is because print (and println) are defined so that if the argument is a reference to an instance, then the associated toString method (defined in the class to which the instance belongs) is called.


4.1 The Default toString Method

Every class created in Java is a descendant of the "top class" Object. This class has in it (amongst other things) a "toString" method that is inherited by all its sub classes (i.e. every Java class). When called using a particular instance this will produce the name of the class to which the instance belongs, the character '@' and a hashcode for the instance (in hexadecimal notation). For example:

Circle@cf398



5. EXAMPLE

The code in Table 1 presents a revised version of the Circle class with "set" and "toString" methods. The accompanying application class is presented in Table 2.

// CIRCLE CLASS
// Frans Coenen
// Tuesday 2 March 1999
// The University of Liverpool, UK
// Revised Thursday 29 November 2001

class Circle {

    // ------------------- FIELDS ------------------------

    private double circleRadius;
    private double circleCircum;
    private double circleArea;

    // ------------------ CONSTRUCTORS -------------------

    /* None */
	
    // ------------------ METHODS ------------------------

    /* Set methods */

    public void setCircleRadius(double cRad) {
        circleRadius = cRad;
        }

    /* Calculate circumference */

    public void calcCircleCircum() {
    	circleCircum = 2.0*Math.PI*circleRadius;
    	}
	
    /* Calculate area */

    public void calcCircleArea() {
    	circleArea = Math.PI*circleRadius*circleRadius;
	}
	
    /* Get methods */

    public double getCircleRadius() {
        return(circleRadius);
        }

    public double getCircleCircum() {
        return(circleCircum);
        }

    public double getCircleArea() {
        return(circleArea);
        }

    public String toString() {
        return("Circumference = " + circleCircum + "\nArea          = " +
                  circleArea + "\n");
        }
    }

Table 1: Revised circle Class definition

// CIRCLE APPLICATION CLASS
// Frans Coenen
// Tuesday 2 March 1999
// The University of Liverpool, UK
// Revised Friday 30 November 2001

import java.io.*;
import Circle;

class CircleApp {

    // ------------------- FIELDS ------------------------

    static BufferedReader    keyboardInput = new
                           BufferedReader(new InputStreamReader(System.in));

    // ------------------ METHODS ------------------------

    public static void main(String[] args) throws IOException {
    	double radius;
	
	// Input radius
	
    	System.out.print("Input a radius (double): ");
	radius = new Double(keyboardInput.readLine()).doubleValue();
	
	// Create new Circle instance using default constructor and
	// set radius value
	
	Circle newCircle = new Circle();
	newCircle.setCircleRadius(radius);
	
	// Calculate circumference and area
	
	newCircle.calcCircleCircum();
	newCircle.calcCircleArea();
	
	// Output result
	
	System.out.print(newCircle);
	}
    }

Table 2: Revised Circle Class application program




Created and maintained by Frans Coenen. Last updated 04 December 2001