JP chap 8, 9.5, 9.10, 9.13, 11.7, 14.
Slide handouts 2.
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.
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"; } }
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.
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.
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.
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!).
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.