A Java application consists of classes and their members. Classes exist in packages but can also be nested
inside other classes.
main() method
Whether it is a console or a graphical interface application the program must have an entrypoint of some
sort. The entrypoint of the Java application is the main method. There can be more than one class with main
method, but the main class is always defined externally (e.g. in a manifest file). The method must be static
and is passed command-line arguments as an array of strings. Unlike C++ or C# it never returns a value and
must return void.
public static void main(String[] args) {
}
package
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,
java.io.InputStream is a fully qualified class name for the class InputStream which is located in the
package java.io.
A package is declared at the start of the file with the package declaration:
package myapplication.mylibrary;
public class MyClass {
}
Classes with the public modifier must be placed in the files with the same name and java extension and put
into nested folders corresponding to the package name. The above class myapplication.mylibrary.MyClass
will have the following path: "myapplication/mylibrary/MyClass.java".
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
including 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.
import java.util.Random; // Single type declaration
public class ImportsTest {
public static void main() {
/* The following line is equivalent to
* java.util.Random random = new java.util.Random();
* It would've been incorrect without the import declaration */
Random random = new Random();
}
}
Import-on-demand declarations allow to import all the types of the package, in the case of type import, or
members, in the case of static import, as they are mentioned in the code.
Static import declaration
This type of declaration has been available since J2SE 5.0. Static import declarations allow access to static
members defined in another class, interface, annotation, or enum; without specifying the class name:
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("Hello World!");
and would have been incorrect without the import declaration. */
out.println("Hello World!");
}
}
Import-on-demand declarations allow to import all the fields of the type:
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. */
Enum constants may also be used with static import. For example, this enum is in the package called
screen:
public enum ColorName {
RED, BLUE, GREEN
};
It is possible to use static import declarations in another class to retrieve the enum constants:
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;
}
}
inside other classes.
main() method
Whether it is a console or a graphical interface application the program must have an entrypoint of some
sort. The entrypoint of the Java application is the main method. There can be more than one class with main
method, but the main class is always defined externally (e.g. in a manifest file). The method must be static
and is passed command-line arguments as an array of strings. Unlike C++ or C# it never returns a value and
must return void.
public static void main(String[] args) {
}
package
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,
java.io.InputStream is a fully qualified class name for the class InputStream which is located in the
package java.io.
A package is declared at the start of the file with the package declaration:
package myapplication.mylibrary;
public class MyClass {
}
Classes with the public modifier must be placed in the files with the same name and java extension and put
into nested folders corresponding to the package name. The above class myapplication.mylibrary.MyClass
will have the following path: "myapplication/mylibrary/MyClass.java".
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
including 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.
import java.util.Random; // Single type declaration
public class ImportsTest {
public static void main() {
/* The following line is equivalent to
* java.util.Random random = new java.util.Random();
* It would've been incorrect without the import declaration */
Random random = new Random();
}
}
Import-on-demand declarations allow to import all the types of the package, in the case of type import, or
members, in the case of static import, as they are mentioned in the code.
Static import declaration
This type of declaration has been available since J2SE 5.0. Static import declarations allow access to static
members defined in another class, interface, annotation, or enum; without specifying the class name:
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("Hello World!");
and would have been incorrect without the import declaration. */
out.println("Hello World!");
}
}
Import-on-demand declarations allow to import all the fields of the type:
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. */
Enum constants may also be used with static import. For example, this enum is in the package called
screen:
public enum ColorName {
RED, BLUE, GREEN
};
It is possible to use static import declarations in another class to retrieve the enum constants:
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;
}
}
No comments:
Post a Comment