| Test Version: GWT 2.0 and above |
Use of “main” method
Each Java class can include a “main” method to perform class execution. This feature is especially useful for experimenting, testing and debugging the pertaining code within the class without resorting to using helper class. Generally it is more efficient and productive to test a particular class (if one already pinpoint the problematic class) rather than to test a whole application that has more overheads and which takes longer time to run. By including a “main” method in class, other developers can also quickly experiment with different API of the code within the class without creating additional testing helper classes. In addition, it is easier to build a reliable application if the classes that make up the application are reliable themselves. Apart from this, the use of “main” method will also encourage one to design the class to be “simplified” so that it can easily test from “main” method.
| public static void main(String[] args) // Java’s main method |
That to say that the “main” method is not a replacement for unit testing, and vice versa. Their intent and usage are different. Unit testing is more formal whereas the “main” method is less formal. Many test cases are included in unit testing and is run periodically to ensure that nothing break the code and create bug from modified code as in the case of time-consuming build process. Unit testing is usually automated and does not involve GUI interaction since the unit testing can be invoked in command-line prior to software development process like build process or check perform by version control system after code check-in.
On the other hand, the “main” method does not subject to such intense test and is not run unless one modify that class and want to have a quick test ensure correct behaviour and result. Developers are free to change whatever in the “main” method of the class during development as long as the “main” method of the class is not used to start up the application, and not call by other classes (It is bad practice to reference and use “main” method from other class.).
onModuleLoad: GWT’s version of “main” method
For those who create Java desktop application can benefit from the use of Java’s main method to help build reliable application. However, for those who use GWT to build desktop-like web application may not apparently take advantage of the “onModuleLoad” method as main method as the official GWT documentation did not actively promote it as equivalent of “main” method but rather frequently utilise it as the EntryPoint interface method to run a application, giving the impression that the onModuleLoad method is only applicable for class that use to start up a GWT application, and nothing more. That perception is further entrenched when GWT examples often use the following onModuleLoad signature to run the class that implements the EntryPoint interface.
|
public class AppClass implements EntryPoint public void onModuleLoad() { |
In reality, GWT team has in fact make “onModuleLoad” method a lot more flexible and capable of behaving like Java’s “main” method.
Foremost, the following behaviours of “onModuleLoad” method make it possible to perform like Java’s “main” method".
- A class need NOT implement EntryPoint interface at all in order to be run as EntryPoint class. As long as the public “onModuleLoad” method is presented in the class, GWT will be able to run it. EntryPoint interface is optional but it is a good practice to implement it for the class that need to start the GWT application. In this way, one can easily use IDE to discover all classes that implement the EntryPoint interface (eg Intellij’s Find Usage)
- A “onModuleLoad” class need not be public in order to be run.
- It is possible to add a static qualifier to “onModuleLoad” method so it looks and behaves like Java’s “main” method. It is important that the “onModuleLoad” method is public if it did not implement the EntryPoint interface (all interface method is public).
- If the following signature is used,
public void onModuleLoad() // Instance method
The class must have a no-default-argument constructor (public, default, private) since GWT will attempt to instantiate the class using the default argument and after which it will call the “onModuleLoad” method. Otherwise, GWT will throw a NoSuchMethodException complaining that class has “no default (zero arugment) contructor”. Hence, using this signature enforce the “runnable” class to have a default constructor, and one has no control over how the class can be run (since GWT instantiates the class automatically and call the onModuleLoad) and designed (eg developers may not want a default constructor)
class TestApp // implements EntryPoint <= Optional
{
TestApp()
{ // This will always be called by GWT first }
// It is not good practice to call this method by other classes.
public void onModuleLoad()
{ // After class instantiation, this method will be called }
}
- On the other hand,
public static void onModuleLoad() // class method
behaves like Java’s “main” method. When the static qualifier is used, GWT will NOT instantiate the class but rather run it as class method (eg AppClass.onModuleLoad() ). This ramification means that one can decide how the class can be instantiated and how the constructor can be designed as in the example. This signature is more natural and preference to use since it conforms to the signature of the ubiquitous Java’s “main” method (public static void main(String[] args).
Note that if static onModuleLoad is used, the class cannot implement EntryPoint interface since interface method is for instance method not class method.
|
class Employee Employee(String _name, int _age, String _job) // GWT will call this class method without instantiating Employee class public static void onModuleLoad() _lEmployees.add(new Employee(“Ali”, 23, “driver”)); GWT.log(“size:” + lEmployees .size()); |
- There might be concern that having “onModuleLoad” method will make the resultant JavaScript file larger after Java-to-JS compilation. However GWT is smart to remove any “dead code” that is not used and thus “onModuleLoad” method will be removed during compilation as long as it is not referenced by other classes.
Just like one will often use “main” method to test classes in Java application, one can also leverages on “onModuleLoad” in GWT to perform similar function.
In Summary, this is the suggested guidelines:
- Only implement EntryPoint interface for the class that essentially use to start up a GWT Web application. That means this is the class that implement the “onModuleLoad” instance method since interface method cannot be static.
public void onModuleLoad() // EntryPoint Interface method
- For other classes, use the “onModuleLoad” class method
- There should be one entry-point tag in the GWT XML module file for project.
public static void onModuleLoad() // Behaving just like “main” method
Challenges
However, the use of “onModuleLoad” method as “main” method may feel unnatural since IDE currently does not support running the “onModuleLoad” class the same way as “main” method (eg Run directly from IDE). Currently, this is more a manual process whereby one will need to switch the entry-point tag explicitly pointing to whatever class that is to run in GWT module file.
* However, I am currently developing the switching application in AutoItScript that allows one to use it in Eclipse and Intellij that at least allow user to change the entry-point with a shortcut key easily without relying on IDE’s plug-in. This will be further elaborated in coming Part2 of this tip.



How I can call onModuleLoad from command line, is it still the same ?
i need to know how to make a code without and error mssg.pls can someone reach me on 08183444424 or+2348183444424,pls help me and god wld help- you.and i want to know how to create a good constructor.
Ok, I’ll try to test the module,, what if there is an error I will beg the solution.
thanks
Nice coder.. maybe i will learn more.. thanks!