How to Use @TypeChecked vs. @CompileStatic in Groovy

  • google plus

In Brief...

Groovy programs are often run as interpreted scripts. A nice advantage of this mode of execution is that typographical errors such as misspelling of variable names are caught as soon as the script is run. But Groovy programs can also be compiled and compilation of Groovy scripts might not reveal a typographical error. In these situations the @TypeChecked and @CompileStatic annotations can be quite useful. Either of the two annotations will ensure the references are correct at compile time. The difference is @CompileStatic will force a compile of a class or method into bytecode at runtime even if the script is run in an interpreted mode whereas @TypeChecked does not invoke the compiler. If you run your scripts using the Groovy interpreter then in most case you probably won't need either annotation. But understanding how they work is important so that you'll know how to use the annotations should the need arise.

To learn how to use the @TypeChecked and @CompileStatic annotations, follow these 12 steps:


  1. Open your text editor and type in the following Groovy statements:
    import groovy.transform.*;
    class Person {
        String fullName  
        def display(){
    		// deliberate incorrect case on the next line:
            println "Hello $FullName"  
    static main (args) {
    	new Person(fullName : "Stephen").display()
    Note that for the moment the annotations are commented out. Therefore, we will run the script without the benefit of either annotation. Also note that $FullName is deliberately cased incorrectly (i.e., the first letter is upper cased) so you can demonstrate that typos are not always caught be the compiler.
  2. Save your file as TypeCheckedvsCompileStatic.groovy.
  3. In the command prompt, type in the command to interpret and run your script:
    Run script with error
    Notice that the interpreter has caught the error, as we would expect.
  4. Next, compile the script in the command prompt:
    Compile script without error
    Clean compile! But wait...the script was terminated with an error in the previous step. Obviously we need to resolve this issue.
  5. Return to the edit session on the script. Uncomment the @TypeChecked annotation by removing the two leading slashes:
    import groovy.transform.*;
  6. Save your changes.
  7. Now try to compile the script:
    Compile script with error
    Better... the compiler caught the error! The same behavior would be observed if you uncommented the @CompileStatic annotation. Feel free to uncomment the @CompileStatic (and comment out the @TypeChecked annotation) and try out a compile. The compile will fail.
  8. Let's fix the problem so we can end on a more successful note. In your edit session change $FullName to $fullName. Comment out both annotations.
  9. Save your changes.
  10. In the command prompt, type in the command to interpret and run your script:
    Run script with no error
    The script works fine now.
  11. Next we will ensure that the compiled program works. Compile the script:
    Compile script without error
  12. Test your compiled program too to see it in action. In your command prompt, type in the command to run your program with the Java runtime launcher (note: change the path of the Groovy embeddable jar file shown below to the path on your computer):
    Run script with no error
    The result of executing the class file with the Java runtime launcher is identical to the output when running the script through the interpreter.

Author: Stephen Withrow

Stephen has over 30 years' experience in training, development, and consulting in a variety of technology areas including Java, C, C++, XML, JavaScript, AJAX, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen is a published writer in both technical and non-technical endeavors. Stephen received an undergraduate degree in Computer Science and Physics from Florida State University.