next up previous contents
Next: Non-static methods Up: 8 Object oriented programming Previous: Introduction to object orientation   Contents

Creating an object

Objects are not dissimilar to variables or arrays in some ways. Like arrays they are reference data types, and so may be passed to and from methods in the same ways as arrays. The major difference is that the programmer can define the form that the object takes -- such as how many values it has and methods that are particular to objects of that type13. To see how this works, read carefully though the following example.

Before creating any objects, we must write a sort of blueprint or template for them. This `blueprint' is the class. Previously you have been using classes really only as a way of grouping together methods in a program. The classes that act as `templates' for objects are behaving differently from the ones you have used before because we now begin to omit the word static in class variable and/or method declarations.14

It is possible to imagine many scenarios when you might want to perform calculations using complex numbers. It would be useful then to create objects that behave in the ways that complex numbers do. Here is a class Complex that defines a complex number.

/**
 * Purpose: A class from which objects can be created, which behave
 *          like complex numbers.
 *          NB: This class should be saved in the file Complex.java
 */
public class Complex
{
    // two non-static class variables
    // representing the real and imaginary parts of a complex number
    public double realpart;
    public double imagpart;
}

Then in any program where you wish to use complex numbers you can create an object of this class. The object, just like a variable, can be given a name of your choosing. To create an object of the class Complex write, in the method of another class:

Complex a;            // creates a reference "a"  (c.f. arrays)
a = new Complex();    // creates a new object of class Complex
                      //     and sets "a" to refer to it

// often this is all written on one line
Complex b = new Complex();

Each object created from this class will have its own realpart and imagpart, and you can refer to these in your program by adding .realpart or .imagpart to the end of the object name. Thus a program that created a Complex object, gave it values and printed them to the screen would look like this.

/**
 * Purpose: Demonstrating how to create an object.
 */

public class ObjDemo
{
    public static void main(String[] args)
    {
        // create an object of type Complex
        Complex a = new Complex();

        // give object values 
        a.realpart = 1.2;
        a.imagpart = -5.9;

        // print to screen
        System.out.println(a.realpart + " + " + a.imagpart + "i");
    }
}

As with any programs that involve more than one class (and therefore more than one file), compiling the file that contains the main method will automatically compile any other files needed for the program to work.

Exercise 8.1


(5 marks)



Type in the Complex class given above and save the file as `Complex.java'. Write your own short program (main method only) similar to `ObjDemo' above that creates two Complex objects and gives them values, then adds them together by refering to the real and imaginary parts as a.realpart and a.imagpart, and prints out the result. Put the program listing of ObjDemo and output showing your tests in you lab book.



This might seem quite a long-winded way of adding two complex numbers, and indeed it is. Fortunately, using objects there is a simple way to do this that is considerably more versatile.


next up previous contents
Next: Non-static methods Up: 8 Object oriented programming Previous: Introduction to object orientation   Contents
RHUL Dept. of Physics