Java Package and import statement
-
Package: Used to categorize classes that complete different functions, and put them under different catalogs (packages).
-
Naming rules for packages: Generally speaking company domain name reversal as package name. For package names, each letter needs to be lowercase.
-
If the package is not used when defining a class, then Java will assume that the class we define is in the default packages.
-
There are two ways to compile a Java source file with the package declaration:
1). Compile directly and then manually build the directory structure based on the package name defined in the class, and finally put the generated class file into the directory structure (rarely used, more cumbersome).
2). Use the compile parameter-D, as javac-d. source files. Java, so that after compiling, the compiler will automatically help us build the directory structure of the package.
-
There are two package names, respectively, aa.bb.cc and AA.BB.CC.DD, so we call the latter the former child package.
-
Import, import the various classes separated by the package, so that the compiler can find the required classes.
-
Import Syntax: Import package path name. class name;
-
Import Com.sun.*, which represents all classes under the Com.sun package.
Import aa.bb.* does not import the class under the aa.bb.cc package, which is required to write:
Import aa.bb.*;
Import aa.bb.cc.*;
-
Questions about the order of the package, import, class:
1). The first need to define the package (packages), optional;
2). Next, import is used, optional;
3). Then the definition of class or interface.
-
If two classes are under the same package, then you do not need to import and use them directly.
Java record -20-package and import statement