Initialization and deallocation methods, The structures of init and awake – Apple WebObjects 3.5 User Manual
Page 46

Chapter 3
Common Methods
46
Initialization and Deallocation Methods
Like all objects, WOApplication, WOSession, and WOComponent implement
initialization methods (or constructors in Java). Because most subclasses require
some unique initialization code, these are the methods that you override most
frequently. In WebScript, the initialization methods are
init
and
awake
. In Java, the
initialization methods are the constructor for the class and
awake
.
Both
init
and
awake
perform initialization tasks, but they are invoked at different
times during an object’s life. The
init
message (or the constructor in Java) is sent
once, when the object is first created. In contrast,
awake
is sent at the beginning
of each cycle of the request-response loop that the object is involved in. Thus,
it may be sent several times during an object’s life.
Complementing
awake
and
init
are the
sleep
and
dealloc
methods. These methods
let objects deallocate their instance variables and perform other clean-up tasks.
The
sleep
method is invoked at the end of each cycle of the request-response
loop, whereas the
dealloc
method is invoked at the end of the object’s life.
The
dealloc
method is used primarily for Objective-C objects. Standard
dealloc
methods in Objective-C send each instance variable a
release
message to make
sure that the instance variables are freed. WebScript and Java, because they
have automatic garbage collection, usually make a deallocation method
unnecessary. If you find it necessary, you can implement
dealloc
in WebScript and
finalize
in Java.
The Structures of init and awake
The
init
method must begin with an invocation of super’s
init
method and must
end by returning
self
.
- init {
[super init];
/* initializations go here */
return self;
}
Likewise, in Java, the constructor must begin with an invocation of the
superclass’s constructor (as with all Java classes):
public Application() {
super();
/* initializations go here */
}
The
awake
method has no such structure. In it, you don’t need to send a message
to
super
or return anything.