Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Java syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Static import declaration==== {{Main|Static import}} This type of declaration has been available since [[J2SE 5.0]]. [[static imports|Static import]] declarations allow access to static members defined in another class, interface, annotation, or enum; without specifying the class name: <syntaxhighlight lang="java"> import static java.lang.System.out; //'out' is a static field in java.lang.System public class HelloWorld { public static void main(String[] args) { /* The following line is equivalent to System.out.println("Hi World!"); and would have been incorrect without the import declaration. */ out.println("Hello World!"); } } </syntaxhighlight> Import-on-demand declarations allow to import all the fields of the type: <syntaxhighlight lang="java"> import static java.lang.System.*; /* This form of declaration makes all fields in the java.lang.System class available by name, and may be used instead of the import declaration in the previous example. */ </syntaxhighlight> Enum constants may also be used with static import. For example, this enum is in the package called <code>screen</code>: <syntaxhighlight lang="java"> public enum ColorName { RED, BLUE, GREEN }; </syntaxhighlight> It is possible to use static import declarations in another class to retrieve the enum constants: <syntaxhighlight lang="java"> import screen.ColorName; import static screen.ColorName.*; public class Dots { /* The following line is equivalent to 'ColorName foo = ColorName.RED', and it would have been incorrect without the static import. */ ColorName foo = RED; void shift() { /* The following line is equivalent to if (foo == ColorName.RED) foo = ColorName.BLUE; */ if (foo == RED) foo = BLUE; } } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)