Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Groovy's static import capability allows you to reference imported classes as if they were static methods in your own class. This is similar to Java's static import capability but works with Java 1.4 and above and is a little more dynamic than Java in that it allows you to define methods with the same name as an imported method as long as you have different types. If you have the same types, the imported class takes precedence. Here is a sample of its usage:

The first static import illustrates defining LIGHT_GRAY as if it was defined locally as a static field.
The next two examples show renaming (called aliasing) of a field and a method respectively.
The final example illustrates wild-carding for fields and methods and also selecting between the locally defined toHexString and imported toHexString based on parameter matching.

As another example, here is how to statically import some of the Math functions:

  • No labels