Java. Packages. Using packages in Java. The import and package directives. Compiled modules (*.java). Intermediate .class files. Project structure. Using standard Java libraries




Packages. Using packages in Java. Directives import and package. Compiled modules (*.java). Intermediate .class files. Project structure. Using standard Java libraries


Contents


Search other websites:

1. What is a package?

Each project in the Java language uses so-called packages in its work. A package is a separate module (namespace), to which corresponds the directory of the same name (folder). The package contains libraries (groups) of classes. These classes are combined in the same namespace or package. The number of classes implemented in the package is unlimited. The classes that are declared in the package are implemented in files with the extension *.java.

Packages can have a variety of levels of attachment (subdirectories, subfolders). At different levels of the package nesting, the names can be repeated.

2. How to connect a package to an existing project in Java Eclipse? The structure of the project that contains the packages

In the Java Eclipse system, the package connection is a standard command. A project must be created in advance. The project can contain an arbitrary number of packages. Each package can have an variuous number of classes.

To create a package in a certain project, use the command

File -> New -> Package

As a result, the window shown in Figure 1 opens. The window displays the name of the project and the package that will be placed in this project.

Java Eclipse. The "New Java Package" window. Creating a package in project

Figure 1. The “New Java Package” window. Creating a package called MyPackage in the MyProject03 project

After selecting the Finish button, the following occurs. A special folder (directory) with the same name corresponds to the project named MyProject03. Several subdirectories (subfolders) are created in this directory. One of them is a subdirectory called src. In this subdirectory, the MyPackage directory is created, corresponding to the created package.

3. What is the general form of calling a class from a package? Example

To access the class name from the package, use the following general form:

PackageName.ClassName

where

  • PackageName – the name of the package in which the class named ClassName is implemented;
  • ClassName – the name of the class.

If the PackageName1 package contains a subfolder (subpackage) named PackageName2, then access to the class named ClassName looks like this:

PackageName1.PackageName2.ClassName

Similarly, access to the class is formed in the case of more complex nesting levels.

For example. Suppose we have a package named Figures. In the package, two classes with the names Circle and Rectangle are implemented. Then, the code fragment that creates the objects of these classes will be as follows:

// ...

Figures.Circle circle = new Figures.Circle();
Figures.Rectangle rect = new Figures.Rectangle();

// ...

4. An example that demonstrates the use of one class name in different packages and in different directories (folders)

Figure 2 shows the hierarchy that packages can form.

Java Eclipse. Package Explorer window. Displaying packages in the project

Figure 2. The MyProject03 project includes three packages named MyPackage01, MyPackage02, MyPackage02.MyFolder02

As you can see from the figure, 3 packages with the names MyPackage01, MyPackage02, MyPackage03 were created in the MyProject03 project. Each of the packages has a class with the same name MyClass01. Thus, the same names are separated.

Below is the demo code that creates the MyClass01 class objects, which are located in different packages (folders), according to the diagram in Figure 2.

// 1. An object of class MyClass01 is created from MyPackage01
MyPackage01.MyClass01 mc0101 = new MyPackage01.MyClass01();

// 2. An object of class MyClass01 is created from MyPackage02
MyPackage02.MyClass01 mc0201 = new MyPackage02.MyClass01();

// 3. An object of class MyClass01 is created from MyPackage02.MyFolder02
MyPackage02.MyFolder02.MyClass01 mc020201 = new MyPackage02.MyFolder02.MyClass01();

As you can be seen from the program code, the class is accessed from the package using the separator ‘ . ‘ (dot). In order to reduce very large lines of access to the class name, the import directive is used.

5. What is the purpose of the import directive? General form. An example

The import directive allows you to shorten very long rows of accesses to classes (interfaces, enumerations) that are implemented in packages.

In the simplest case, the general form of the ‘import’ directive is as follows:

import PackageName.ClassFileName;

where

  • PackageName – the name of the package that connects;
  • ClassFileName – the name of the file with the extension *. java, in which the class or group of classes to which you want to access by abbreviated name is implemented.

For example. Let the package Figures be specified in some project. In the Figures package, one class with the name Circle is implemented. The class is placed in the file “Circle.java”. The source code for the Circle.java file that implements the Circle class is:

// file "circle.java"
package Figures; // the Figures package

// A class that describes a circle on the coordinate plane
public class Circle
{
    double radius;
    double x, y;

    // to set the Circle() constructor visible outside the package,
    // the 'public' access must be set
    public Circle()
    {
        x = y = 0;
        radius = 1;
    }

    // the access specifier 'public' is mandatory,
    // if you want the Set() method to be visible outside of the package
    public void Set(double x, double y, double radius)
    {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }

    // method that returns the area of a circle
    // the 'public' access specifier is used before the method
    public double Area()
    {
        return 3.1415*radius*radius;
    }
}

If you want to access the methods of the Circle.java file from the Figures package from any other class that is located in another package of this project, you must first indicate:

import Figures.Circle;

The following is an example of connecting and using the Circle class from another package and another class.

// file "MyClass.java"
package OtherPackage;

// connecting the Circle class for shortened access
import Figures.Circle;

public class MyClass
{
    public static void main(String[] args)
    {
        // The abbreviated form of access to the class name is used
        Circle c = new Circle();
        double d;

        d = c.Area(); // d = 3.1415
        System.out.println("d = " + d);
    }
}

In the above code, a Circle object with the name c is created. However, with the help of the ‘import’ directive, the class name is indicated in a convenient (abbreviated) form:

...

Circle c = new Circle();

...

If you remove the line in the OtherPackage package

import Figures.Circle;

then the object of the Circle class would have to be created as follows

Figures.Circle c = new Figures.Circle();

6. How in the import directive specify that all classes from this package can be used?

There are cases when several classes are implemented in the package (files with *.java extensions) and there is a need to connect the whole set of classes. In this case, the general form of the ‘import’ directive can be:

import Package.*

where

  • Package – a package name with a class library.

7. Keyword ‘package’

The ‘package’ keyword specifies the name of the library, which can include a different number of compiled modules. Compiled modules are files with the .java extension.

If the module (file with the .java extension) specifies a string like

package PackageName;

then this means that this module is part of the library named PackageName.



8. What is a compiled module in Java?

Each compiled module is a file that contains the source code in the Java language. This file has the extension “*.java”. The file name is the same as the name of the public class that is implemented in this module. This requirement is mandatory.

Each compiled module can contain no more than one public class. The compiled module is included in the package. The compiled module is included in the package. There can be any number of compiled modules in a package.

9. What are the requirements for the implementation of the compiled module (.java)?

The following requirements are imposed on the implementation of the compiled module:

  • the name of the compiled module must have the .java extension;
  • the name of the compiled module without the .java extension must match the name of the public class that is implemented in this module;
  • the name of the compiled module and the name of the public class implemented in it must start with a uppercase letter;
  • in the compiled module there can be no more than one public class. If there are other (additional) classes in the compiled module, then they must be hidden (without the public modifier).

10. How many classes can be implemented in the compiled module?

A compiled module can have any number of classes. However, there must be only one public class. The name of this class must match the name of the compiled module.

11. What is the purpose of .class files?

Files with the extension .class are obtained as a result of compiling files with the extension .java (compiled modules). Files with the extension .class are intermediate files, which are then combined by the linker into the executive module.

For example, in the Delphi programming language, the temporary compiled files had the extension *.dcu. In the C++ programming language, the temporary compiled files had the extension .obj. After linking out of these files, we got *.exe file (executable module).

After that, the files with the extension .class are combined into a package and compressed by the JAR utility. This is all done by the Java interpreter.






12. What are the benefits of using the ‘package’ and ‘import’ directives?

Using the package and import directives makes it convenient to divide the namespace so as to prevent conflicts between different Java class developers.

13. An example that demonstrates the structure of a project and the placement of compiled modules

Let Java Eclipse create a project named Project03. The project is located in the default folder that is used by the Java Eclipse system. The two packages with the names Figures, OtherPackage are created in the project. In packages the compile modules are implemented with the names Circle.java and MyClass.java. Figure 3 shows the structure of the Project03 project of the Java Eclipse system.

Java Eclipse. Package Explorer window. Displaying the packages in the project

Figure 3. Displaying the Project03 project structure in the Package Explorer window

Circle.java file (module) involves a class called Circle. The MyClass.java file involves a MyClass class. In the MyClass class from the OtherPackage package (see Figure 3), the Circle.java module is connected using the directive

import Figures.Circle;

If the compiler encounters the import directive, it generates the full name, taking into account the path to the directory that is written by default on the system.

C:\Programs\Java + \Project03\src + \Figures\Circle.java

thus, the full name of the Circle.java module is obtained:

C:\Programs\Java\Project03\src\Figures\Circle.java

If desired, the default path to the directory (folder) can be changed.

14. An example of connecting a class from the standard Java library

To access the class name from the standard Java library, use one of two methods:

  1. Use the full form or full class name in the library.
  2. Use the abbreviated form of the class name. In this case, the full name of the package with the desired class can be specified explicitly with the import directive.

The complete form of accessing the name of the required class in the simplest case looks like this

java.PackageName.ClassName

where

  • PackageName – the name of the package or standard Java library (eg, util, math, applet, etc.). Packages or libraries can have nested levels. If the libraries contain attachments, they are separated by the symbol ‘ . ‘;
  • ClassName – the name of the class (or interface) that is implemented in the PackageName library (for example, ArrayList, BigDecimal, etc.).

For example. A code snippet that uses the Vector class from the standard Java library. In this case, the standard Java library is named java.util.

...

// access to the Vector class from the package java.util
java.util.Vector v = new java.util.Vector();

v.clear();
v.add("string - 01");
v.add("string - 02");
v.add("string - 03");

int n;
n = v.size(); // n = 3

String s;
s = (String)v.elementAt(0); // s = "string - 01"

...

In the above code, the Vector class is referenced by the full name:

java.util.Vector


Related topics