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!
==Program structure== Java applications consist of collections of classes. Classes exist in packages but can also be nested inside other classes. ===<code>main</code> method=== {{Main|Entry point#Java}} Every Java application must have an entry point. This is true of both graphical interface applications and console applications. The entry point is the <code>main</code> method. There can be more than one class with a <code>main</code> method, but the main class is always defined externally (for example, in a [[manifest file]]). The <code>main</code> method along with the main class must be declared <code>public</code>. The method must be <code>static</code> and is passed command-line arguments as an array of strings. Unlike [[C++]] or [[C Sharp (programming language)|C#]], it never returns a value and must return <code>void</code>. <syntaxhighlight lang=Java> public static void main(String[] args) { } </syntaxhighlight> ===Packages=== Packages are a part of a class name and they are used to group and/or distinguish named entities from other ones. Another purpose of packages is to govern code access together with access modifiers. For example, <code>java.io.InputStream</code> is a fully qualified class name for the class <code>InputStream</code> which is located in the package <code>java.io</code>. A package is declared at the start of the file with the <code>package</code> declaration: <syntaxhighlight lang=Java> package myapplication.mylibrary; public class MyClass { } </syntaxhighlight> Classes with the <code>public</code> modifier must be placed in the files with the same name and {{mono|java}} extension and put into nested folders corresponding to the package name. The above class <code>myapplication.mylibrary.MyClass</code> will have the following path: <code>myapplication/mylibrary/MyClass.java</code>. ===Import declaration=== ====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> ====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)