Lecture 14, OOP - instance creation & garbage collection

Reading

JP chap 8, 9.5, 9.10, 9.13, 11.7, 14.

Slide handouts 2.

Study Guide.

This lecture will all be about how to create objects, and how to automatically get rid of objects that are no longer needed. 

The main thing about object creation is the order in which objects are initialized, and different ways in which one can control and utilize that order.  

Objects are created using a special kind of expression, the "new" expression. The new expression returns a reference to a new object. The new expression is defined in section 11.7. After the object has been created, it is initialized, and this is the interesting part, which is described in sections 9.9 and 9.10. To make it all come together with inheritance, so we can make new object from classes that inherit from other classes, we take into account what is written in 9.5. Notice, there is an error in the book. On page 22, 4rt line from the bottom, it says that "Non static initializer blocks are executed after the the constructor when an object is created", this is not true. What is true is the rules written in section 9.9. 

Instantiation of arrays is a whole topic in its own, which is what section 8 is all about. 

In the lecture I will also spend some time on a different way to create new objects, namely by cloning existing ones. In some programming languages, one can only clone objects, not create new ones. In Java cloning is possible, but not widely used.

In Java 5.0 a new kind of classes have been added, enum types. Chapter 11 is on enum types. They are not in the slides yet, but will be added over the weekend, and be available at latest Monday morning. 

After this lecture, there is very very little about making objects in Java you will not have heard about.

Even though you as a programmer can not really influence the way in which java get rid of objects, we shall take a look at some of the principles of how automatic memory management, also called garbage collection, is handled. Java is the first mainstream object oriented programming language which incorporates automatic memory management. The principles for this was first developed in research languages in the early 1990'ies.

The reference http://www.javaworld.com/javaworld/jw-08-1996/jw-08-gc_p.html gives a reasonable good overview of the different problems and strategies for garbage collection. The paper refers to an applet, which can be seen on http://pl.changwon.ac.kr/~pl/seminar/jvm/insidejvm/applets/HeapOfFish.html

Exercises for lecture 2, OOP

  1. (Hand-In) This exercise is about initialization in connection with instance creation. Consider the following classes:
  2. package dk.itu.oop.lecture2;
    
    public class InitializeOrder {
    	public static void main(String[] args){
    		AAA a = new BBB();
    	}
    	
    }
    
    class AAA {
    	private String a = "Hello";
    	private String b;
    	private int x = 17;
    	
    	public AAA(String s){
    		b = s;
    	}
    	
    	public AAA(){
    		b ="Goodbye";
    	}
    }
    
    
    class BBB extends AAA {
    	private String c = "Go'dag";
    	
    	public BBB(){
    		super("G'day mate");
    		c = "Davs";
    	}
    }
    1. In what order are the fields initialized and with what value, when a BBB object is created? Pay special attention to the order of initializers and constructors.
    2. The class CCC might have been defined as:
      class CCC extends AAA {
      	private String d = "Go'dag";
      	
      	public CCC(){
      		super(d);
      	}
      }
      However, this class is not legal Java. It breaks the initialization order in a subtle way. The compiler will discover this, and report the following error message: "InitializeOrder.java: cannot reference d before supertype constructor has been called" in the line containing "super(d);". In what way does the class CCC break the initialization order of Java - try to explain the problem in terms of section 9.9 in Java Precisely.
  3. (Hand-In) This exercise is about simple two-way associations. An example of a simple two-way association is the ownership relation between a Person and a Car. To ensure consistency, the following three rules should be obeyed.
    1. Each car is owned by exactly one person, that is, no car is without an owner, and no car is owned by more than one person.
    2. Each Person can own at most one car.
    3. If a person owns a car, that car is owned by that person, and vice versa.

    It is common for simple two-way associations that one side X must be associated to the other side Y, while Y might be associated to an X.

    1. Write the two classes Person and Car, such that each has a public (declaring it private will make the exercise much harder) reference field that can refer to one of the other kind, that is, class Person as a field Car car, and class Car has a field Person owner. Hint. Use the constructor of Car to make sure that a car is never created without a owner, this should take care of rule 1 & 2 above. 
    2. Add a method void newOwner(Person p) to the car class. It should make p the new owner of this car. Make sure rule 1, 2 & 3 are respected by the method. You may assume that the person p is not the owner of an other car.

    This exercise was on simple two-way associations. General two-way associations is when there is not a one-one relationship between the objects, but a person can own more than one car, or more generally, a car can be owned by several people in a joint ownership. In those situations, rule 3 still must hold. A solution to general associations is not required. Associations are an important part of the UML modelling diagrams. 

  4. In the Pen class from the lecture, change the clone method so that a cloned of a blue pen is red and a clone of a red pen is blue. Clones of green pens remain green. (Notice, this exercise can be solved without you understanding all the code in the file Drawing.java, just examine the clone method!).

  5. Extend the class Person from exercise 2 with a clone method. It should return a clone of the Person. The clone method should be coded such that rule 1, 2 & 3 are respected.