Groovy Documentation

groovy.transform
[Java] Annotation Type Field

java.lang.Object
  groovy.transform.Field

@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.LOCAL_VARIABLE})
@GroovyASTTransformationClass("org.codehaus.groovy.transform.FieldASTTransformation")
public @interface Field

Variable annotation used for changing the scope of a variable within a script from being within the run method of the script to being at the class level for the script.

The annotated variable will become a private field of the script class. The type of the field will be the same as the type of the variable. Example usage:

 @Field List awe = [1, 2, 3]
 def awesum() { awe.sum() }
 assert awesum() == 6
 
In this example, without the annotation, variable awe would be a local script variable (technically speaking it will be a local variable within the run method of the script class). Such a local variable would not be visible inside the awesum method. With the annotation, awe becomes a private List field in the script class and is visible within the awesum method.
Authors:
Paul King
Since:
1.8.0


Method Summary
 
Methods inherited from class java.lang.Object
java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()
 

Groovy Documentation