Home > Java > javaTutorial > body text

Importing packages

Linda Hamilton
Release: 2024-10-02 06:19:01
Original
667 people have browsed it
  • When using a class from another package, you can qualify its name with the package name, but this can be tedious.

  • Full class name qualification becomes cumbersome when packages have deep hierarchies.

  • To make things easier, the Java language offers the import statement, allowing the use of package classes without requiring complete qualification.

  • The import statement makes the members of a package visible, allowing them to be used directly.

  • The general form of the import statement is: import package.classname;.

  • To import the contents of a package, you can use the import statement with an asterisk (*) to include all classes in the package.

  • Example of importing a specific class: import mypack.MyClass;.

  • Example of importing all classes from a package: import mypack.*;.

  • The import statement must be placed right after the package statement (if any) and before any class definition in the source file.

  • Using import allows you to give visibility to the bookpack package, facilitating the use of the Book class without requiring full qualification.

  • To use the Book class, add the import bookpack.* statement; at the beginning of the file.

Importando pacotes

  • Note that you no longer need to qualify Book with the package name.

The above is the detailed content of Importing packages. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template