Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>Groovy's <em>static import</em> capability allows you to reference imported classes as if they were static methods in your own class. This is similar to Java's <em>static import</em> 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:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>import static java.awt.Color.LIGHT_GRAY import static Boolean.FALSE as F import static Calendar.getInstance as now import static java.lang.Integer.* println LIGHT_GRAY // => java.awt.Color[r=192,g=192,b=192] println !F // => true println now().time // => Sun Apr 29 11:12:43 EST 2007 println "Integers are between $MIN_VALUE and $MAX_VALUE" // => Integers are between -2147483648 and 2147483647 def toHexString(int val, boolean upperCase) { def hexval = upperCase ? toHexString(val).toUpperCase() : toHexString(val) return '0x' + hexval } println toHexString(15, true) // => 0xF println toHexString(15, false) // => 0xf</pre></td></tr></table><p>The first static import illustrates defining <code>LIGHT_GRAY</code> as if it was defined locally as a static field.<br /> The next two examples show renaming (called <em>aliasing</em>) of a field and a method respectively.<br /> The final example illustrates wild-carding for fields and methods and also selecting between the locally defined <code>toHexString</code> and imported <code>toHexString</code> based on parameter matching.</p><p>As another example, here is how to statically import some of the Math functions:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>import static java.lang.Math.* println sin(123.456) * cos(456.789) // => 0.24733809349262376</pre></td></tr></table><p><em><u><strong>Note:</strong></u> Groovy does not check beyond your import class if what you statically import exists. If you import a nonexisting method, field or property Groovy will not fail at compile time, but later when executing the compiled code.</em></p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced