Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lecture 1.docx
Скачиваний:
1
Добавлен:
01.07.2025
Размер:
704 Кб
Скачать
☆

8.3. Explicit initialization

Class fields may be explicitly initialized. With the following definition: class Pair {     int a = 1;     int b = 1;     Pair() {         a++;         b++;     } } values of components of newly created objects (using the above constructor) of the class Pair are (2, 2). To initialize a field any expression may be used:

The static method max from the class Math returns the greater of the two arguments.

class Pair {     int a = 2;     int b = Math.max(a, 1) + a + 1;         Pair() { }     } new Pair() creates an object with elements 2, 5.

Initialization of non-static fields takes place before constructor invocation and is performed in the order in which the fields are defined in the class.

Static fields are initialized by the first use of the class. Order of the initialization is determined by the order in which they are defined in the class.

8.4. Packages and imports

A package is a kind of a class library which groups classes having similar functionality: input-output operations, network applications, and so on. Each class belongs to some package. Classes compiled with package declaration package ... belong to the declared package. Classes compiled without any package declaration belong to the default (unnamed) package. Packages have hierarchical structure: aPackage   aSubPackage     aSubPackage For example, the package java.awt contains the subpackage java.awt.event. Hierarchical structure of packages is mapped into directory structure (the consecutive levels of directories in the directory hierarchy correspond to consecutive levels in package hierarchy). Why use packages? Consider an example: class A {     Button b = new Button("Ok");  // new object of the class Button .... } Two cases are possible:

  1. the class Button is defined in the same file (as the class A) or in some other file in the current directory

  2. The class Button is defined somewhere else

In the first case the compiler (and the Java virtual machine) may find the class. In the second case arises the problem: where to search for the class? Moreover, use of the name Button may be ambiguous (is it the Button from the current directory or from somewhere else?). Thus, packages group classes into namespaces and protect against the name conflicts.   Qualified names A qualified name of a class (type) belonging to a named package has a form:

        package_name.class_name

for example:   java.awt.Button class A {  java.awt.Button b = new java.awt.Button("Ok"); .... } For the classes defined in the default (unnamed) package the qualified name is the same as the name of a class (for example: Pair). In general, all class names used in a program should be qualified. Fortunately, the import statement allows using simple names. import java.awt.Button;  // imports the name of the class java.awt.Button class A {  java.awt.Frame f = new java.awt.Frame("Title");  Button b = new Button("Ok");  // use of the simple name of the class java.awt.Button .... } Because the name of the class java.awt.Button was imported, we may use its simplified name in the program. But the name of the class java.awt.Frame was not imported, so we must use its fully qualified name. To import all the class names from a given package we must use an asterisk: import java.awt.*;     // imports all the class names from the package java.awt class A {      Frame f =  new Frame("Title");     // now the simplified name may be used      Button b = new Button("Ok"); .... } The classes String and System used frequently in programs are in the package java.lang.

The package java.lang does not require the import statement. The classes from this package are imported automatically.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]