The recommended way to use the EventActionDispatcher is as follows (per the API docs @ http://struts.apache.org/1.2.9/api/org/apache/struts/actions/EventActionDispatcher.html )
public class MyCustomAction extends Action { protected ActionDispatcher dispatcher = new EventActionDispatcher(this); public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return dispatcher.execute(mapping, form, request, response); } }
Does doing this publish the reference to ‘this’ before the constructor exits? What are the rules governing field assignments outside of methods.
Thanks in advance.
Sincerely, LES
That took 3 people a week to track down once… ‘this’ can be null if the EventActionDispatcher starts a thread or does anything with a thread that causes the ‘this’ to be used.
NEVER pass ‘this’ before the constructor has completed or you run the risk of ‘this’ being null in the case of threading.
What I do is add an ‘init()’ method to my classes that need to do things like that and call it after I create the object.
There are also other subtleties, such as this example:
The safest thing to do is:
You can can call final methods, but you have to be sure that they do not call overrideable methods… and that can mean things break down the road… so safer not to do it.