An introduction to objects and object references



Learning and mastering object-oriented, Java programming can be confusing. In this lesson, some fundamental concepts are introduced that will help you understand what an object is and how to use one.

Until now, the primary use of classes in our programs has been simply to hold the main() method and other methods it might call. We have not used a class to create an object.

To realize the full potential of the Java language, a programmer must be able to:

  1. Define a class from which objects can be constructed. Such classes are said to be "instantiable".

  2. Construct and use objects of an instantiable class

For now, our emphasis is on constructing and using objects of an instantiable class. Defining an instantiable class will be covered later.


An instantiable class

All classes presented thus far, though technically instantiable, would not be useful in constructing objects because their methods were declared static (the implications of which will be covered in a later lesson).

For example, if a company needs to maintain and process customer data, a Customer class can be defined. It can also create other classes (such as Order, Product, Part, Supplier, etc.) to maintain and process other types of data.

For example, a Customer class might have variables for holding a customer's account number, name, address, and credit limit and methods for storing a new address and changing their credit limit.

For example, after it has been defined the Customer class could be used to instantiate an infinite number of Customer objects. Each object would represent a single customer and have its own values for account number, name, address, and credit limit and its methods would store a new address and change the credit limit of its unique customer.

For example, the Customer class may have one constructor that receives initial values for the customer's account number, name, address, and credit limit while another constructor might receive only the customer's name and address.


An object

For example, if the Customer class is used to instantiate three Customer objects, each will have its own instance variables and instance methods:

Customer one

Customer two

Customer three


Bob Smith
123 Oak



Methods to process
object's data



Sue Green
918 Cedar



Methods to process
object's data



James Jones
442 Maple



Methods to process
object's data


The keyword this in Java is a reference to the current object.

Customer current;

creates an object reference with the identifier current that may be used to reference a Customer object. Because no value was assigned to current, it will have the initial value of null (a Java keyword that means no value). In order to use current, a Customer object must be constructed and assigned to it.

An object reference can only reference one object at a time but may be re-used.

where the new keyword obtains memory space for the object and Customer() calls the Customer class constructor method that requires no parameters.

It is possible to both declare and initialize an object reference in a single statement. This is much like declaring and initializing a variable with a single statement. For example,

Customer current = new Customer(3906, "Mary Brown", "118 Maple", 1000);

results in the following actions:

  1. Memory space for a new Customer object is obtained

  2. The Customer constructor method that receives values for the customer's account number, name, address, and credit limit is called to initialize the object's values

  3. The object reference current is created and assigned the location of the newly created Customer object

current.setAddress("835 Pine")

would call the setAddress() instance method of the Customer object referenced by current.

Occasionally, it is convenient to instantiate an object and call one of its methods immediately without ever assigning it to an object reference. For example, the following statement uses Java's packaged Double class to convert the string literal "123.45" to its primitive double value:

double x = new Double("123.45").doubleValue();

The Double class is one of Java's "wrapper" classes (to be covered in a later lesson). All you need to know for now is that memory is obtained for a Double object and the string literal is passed to its constructor. The object's doubleValue() method is then called to retrieve the corresponding primitive value. It is this primitive value that is assigned to the double variable named x. The Double object is never assigned to an object reference and its existence is temporary.

For example, the following statement would schedule the garbage collection of the object referenced by current if no other reference exists for that object:

current = null;

While current would reference nothing at this point, it could later be assigned to another Customer object.

Note that the actual time at which an object is garbage colleted is up to the JVM. There is no way for a program to tell the JVM to "do it now".


Lab exercise for Ferris students

E-mail your answers to this assignment no later than the due date listed in the class schedule.


Review questions

  1. If Order is a class having a constructor method that requires no parameters, which of the following would instantiate an Order object that may be referenced by the identifier temp?  (choose two)

  1. Order temp;
    temp = Order();

  2. temp = new Order();

  3. Order temp;
    temp = new Order();

  4. Order temp = Order();

  5. Order temp = new Order();

  1. Assume that all unseen code is correct and that line numbers are for reference purposes only. If Part is a class having a constructor method that requires two parameters (an int for the part number and a string for the part name), which one of the statements below is true of attempting to compile and execut the following code fragment?

Part x;
x = new Part(3, "Widget");
x = new Part(7, "Framistan");
  1. a compile error will occur at line 1

  2. a compile error will occur at lines 2 and 3

  3. after executing these statements, x will reference the Part object for "Widget"

  4. after executing these statements, x will reference the Part object for "Framistan"

  5. after executing these statements, x will reference the Part objects for both "Widget" and "Framistan"

  1. Assume that Employee is an instantiable class having a constructor requiring no parameters and an instance method named getPayRate() requiring no parameters and returning a double value representing the employee's pay rate. If theEmp currently references an Employee object, which of the following code fragments will display the employee's pay rate and then schedule the object for garbage collection?  (choose two).

  1. System.out.println(theEmp.getPayRate());
    theEmp = null;

  2. System.out.println(Employee.getPayRate());
    theEmp = null;

  3. System.out.println(theEmp.getPayRate());
    theEmp = new Employee();

  4. System.out.println(theEmp.getPayRate());
    theEmp = 0;

  5. System.out.println(Employee.getPayRate());
    theEmp = 0;

  1. True or False: An object reference must always be declared in order to instantiate and access the instance methods of an object.

  1. True

  2. False