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!
====Type import declaration==== A type import declaration allows a named type to be referred to by a simple name rather than the full name that includes the package. Import declarations can be ''single type import declarations'' or ''import-on-demand declarations''. Import declarations must be placed at the top of a code file after the package declaration. <syntaxhighlight lang="java"> package myPackage; import java.util.Random; // Single type declaration public class ImportsTest { public static void main(String[] args) { /* The following line is equivalent to * java.util.Random random = new java.util.Random(); * It would have been incorrect without the import. */ Random random = new Random(); } } </syntaxhighlight> Import-on-demand declarations are mentioned in the code. A "type import" imports all the types of the package. A "static import" imports members of the package. <syntaxhighlight lang="java"> import java.util.*; /*This form of importing classes makes all classes in package java.util available by name, could be used instead of the import declaration in the previous example. */ import java.*; /*This statement is legal, but does nothing, since there are no classes directly in package java. All of them are in packages within package java. This does not import all available classes.*/ </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)