Greetings,
This purposefully overridable method is called from the Resource(Context,
Request, Response) constructor and therefore subclasses that override
init(Context, Request, Response) are in for a big suprise if they try to do
operations on their own fields in the init method. At this point in
execution, the subclass of Resource has not been initialized and so default
field intializers have not been called, not even fields declared final. The
default initializers will be called after init finishes and any subclass
fields set during the init method call will be overwritten with defaults.
This is a gotcha in Joshua Blochs Effective Java pg 80. Constructors of
classes intended to be subclassed should not call overridable methods.
So I think some clarification on when to override init when your class also
supports the three argument resource constructor (assuming you call
super(context,request,response) first in the constructor.) Why is there a
three argument constructor? The Finder.createResource calls the default
constructor then calls init afterwards which is ok because all subclasses
have been initialized by then.
Perhaps there is a way with annotations to convey Variants a resource has
representations for as this is what the init method seems to be of value
for, adding variants to the list.
-Joe
- When and when not to override Resource.init Joe Nellis
-