Chapter 6, Exercise 3
3. Create a simple class. Inside a second class, define a
reference to an object of the first class. Use lazy initialization
to instantiate this object.
Well, Scala doesn't really quite permit lazy initialization
(see the "= _;" phrase in sc's declaration), but it's
substantially the same. Here
are the Java and Lisp versions that Larry wrote.
package x;
class SimpleClass {}
class SecondClass {
private var sc : SimpleClass = _;
def getSC : SimpleClass = {
if (sc == null) sc = new SimpleClass();
sc;
}
}
object c6x3 {
def main ( args : Array[String] ) = {
val c2 = new SecondClass;
W.rn( "sc is " + c2.getSC );
}
}
12:50:50 AM