Chapter 4, Exercises 10-12
Chapter 4, Exercise 10
Exercise 10 asks "Create a class with two (overloaded)
constructors. Using this, call the second constructor
inside the first one."
Seems pretty simple and straightforward.
Goes with
this entry from THECLAPP.
class c4x10c {
var quantity : Double = _;
def this( d: Double ) = {
this();
quantity = d;
}
def this( i: Int ) = {
this( i * 1.0 );
}
}
object c4x10 {
def main ( args: Array[String] ) = {
var obj = new c4x10c( 3 );
Console.println( "obj.q is "+ obj.quantity );
}
}
Chapter 4, Exercise 11
Bruce asks us to "Create a class with a finalize() method
that prints a message. In main(), create an object of your
class. Explain the behavior of your program."
(
Here's the corresponding THECLAPP entry.)
Now, before we get all carried away with finalizers,
let's read
David Chess' takeaways (from two years ago) from
Joshua Bloch's Effective Java:
- Constructors are lousy; use static "newInstance()" methods instead,
- Finalizers are lousy; avoid them in essentially all cases,
- The Serializable interface has serious problems; use it rarely,
- The Cloneable interface is essentially unusable; avoid it,
- Inheritance is messy and dangerous; use composition instead,
- Abstract classes are usually a poor idea; use interfaces instead.
And let's notice that Scala does not have finalizers.
Nonetheless, if we put the finalizer in a java class
and then extend it in our Scala code, we can get close
to the requested code:
class c4x11s extends x.c4x11j {
var j: Int = 42;
}
object c4x11 {
def main ( args: Array[String] ) = {
var obj = new c4x11s;
// c4x is not a member of x.c4x11s
// Console.println( "obj.j " + obj.j + "; obj.c4x " + obj.c4x );
Console.println( "obj.j " + obj.j );
obj = null; // this is the c4x12 addition
System.gc();
}
}
Note that the scala class c4x11s extends a class c4x11j.
Since we need a finalizer for this exercise, we implement
that finalizer in java. Here's c4x11j.java:
package x;
public class c4x11j {
public int c4x = 11;
public void finalize() {
System.out.println( "Finalizing c4x11j." );
}
}
One oddity whose explanation I haven't yet looked up
is that the scala class cannot access the public member
c4x of the java class c4x11j. I probably have to make it
into a property, adding at least some kind of get method.
Anyway, Explain the behavior of your program?
What, explain why it did not invoke the finalizer?
The code in red there gives that away. We still had a live
reference to it, right up until the program went away, and
Java finalizers aren't really very much like "finally" blocks.
This is one of the reasons why Joshua wrote (in David's interpretation)
that "finalizers are lousy; avoid them in essentially all cases".
Chapter 4, Exercise 12
See the red line in c4x11. Break our only link to that
object by setting our reference to null, and then the finalizer
does get invoked.
THECLAPP found some other oddities
involving inner code blocks and other functions, but none
of that was surprising to this old C programmer.
10:36:50 PM