{"id":482,"date":"2008-07-31T16:39:33","date_gmt":"2008-07-31T22:39:33","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/archives\/000482"},"modified":"2008-10-08T08:53:14","modified_gmt":"2008-10-08T14:53:14","slug":"gwt-mini-pattern-bare-bones-entry-point-class","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/482","title":{"rendered":"GWT Mini Pattern: Bare Bones Entry Point Class"},"content":{"rendered":"<p>Don&#8217;t ever let your entry point class do anything. Why? Because the constructor of the entry point class will be called twice. Once when the object is being instantiated so that <code>onModuleLoad<\/code> can be called, and then once if your code does anything useful in the constructor. You can also have a constructor that does nothing active (XHR, modifying the DOM) in which case it won&#8217;t matter that it is called twice (except it will slow down the user&#8217;s computer a little bit.)<br \/>\nHere&#8217;s an example of how not to do it (sorry for the [lack of] code formatting):<\/p>\n<div><code>\u00a0<\/p>\n<pre>public class Display implements EntryPoint {  public void onModuleLoad() {  final RootPanel rootPanel = RootPanel.get(COMPONENT_HOME_ID);  if (rootPanel != null) {  Display fd = new Display();  } }  public Display() { Window.alert(\"new date: \"+new Date()); \/\/ more stuff }  \/\/ more stuff }<\/pre>\n<p>If you were to compile and execute the code above, you'd see two different alerts. Instead you want:<\/p>\n<div><code><\/p>\n<pre>public class DisplayEntry implements EntryPoint {  public void onModuleLoad() {  final RootPanel rootPanel = RootPanel.get(COMPONENT_HOME_ID);  if (rootPanel != null) {  Display fd = new Display();  } }  }  class Display { \/\/ or it could be in a separate java file public Display() { Window.alert(\"new date: \"+new Date()); \/\/ more stuff }  \/\/ more stuff }<\/pre>\n<p><\/code><\/div>\n<p>You can also get around it by having a no-op no arg constructor (which all <code>EntryPoint<\/code> classes are required to have) and one that takes arguments that you'll use in your class to do something.<\/p>\n<p>Tested on Windows, GWT 1.4.62.<\/p>\n<p><\/code><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Don&#8217;t ever let your entry point class do anything. Why? Because the constructor of the entry point class will be called twice. Once when the object is being instantiated so [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,30],"tags":[],"class_list":["post-482","post","type-post","status-publish","format-standard","hentry","category-gwt","category-gwt-mini-patterns"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=482"}],"version-history":[{"count":0,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/482\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}