content
1. Import the class in the package
3. Put the class into the package
Objectives of this section
Bag
1. What is a package
A package is a way of organizing classes.
The main purpose of using packages is to ensure the uniqueness of classes.
For example, you wrote a Test class in your code. Then your colleagues may also write a Test class. If there are two classes with the same name, they will conflict and the code will not compile.
1. Import the class in the package
Java already provides many ready-made classes for us to use
①: For example, to print an array:
public class TestDemo{ public static void main(String[] args) { int[] array = {1,2,3,4,5}; System.out.println(Arrays.toString(array)); } }
To use Arrays, you need to import the package, see the picture:
If the top line of code is not written, an error will be reported, see the picture:
So how to import the above package? When we write the code of Arrays, IDEA will automatically jump out of the options for you to choose. If you select the first item and press Enter, it will help you import the package. Look at the picture:
②: Another example:
The Date class defines the date and is also written by the Java class library.
public class TestDemo { public static void main(String[] args) { java.util.Date date = new java.util.Date();//在我们不导包时候手写 // 得到一个毫秒级别的时间戳 System.out.println(date.getTime()); } }
You can use java.util.Date to introduce the Date class in the java.util package
But this way of writing is more troublesome. At this time, you can use the above way of writing, you can use the import statement to import the package
import java.util.Date; public class TestDemo { public static void main(String[] args) { Date date = new Date(); // 得到一个毫秒级别的时间戳 System.out.println(date.getTime()); } }
Precautions:
You can import a specific class, but not a specific package
: Import the util package and report an error
: Import a specific class
③: Another example:
If you need to use other classes in java.util, you can use import java.util.*
import java.util.*; public class TestDemo { public static void main(String[] args) { Date date = new Date(); // 得到一个毫秒级别的时间戳 System.out.println(date.getTime()); } }
: No error is reported, this * can be understood as a wildcard, which means importing all the classes under this package
Question: There are many classes under util, are they all imported at once? No, when Java handles it, whoever needs it will do it.
④: But we prefer to explicitly specify the class name to be imported. Otherwise, conflicts are prone to occur.
Example:
import java.util.*; import java.sql.*; public class TestDemo { public static void main(String[] args) { // util 和 sql 中都存在一个 Date 这样的类, 此时就会出现歧义, 编译出错 Date date = new Date(); System.out.println(date.getTime()); } } // 编译出错 Error:(5, 9) java: 对Date的引用不明确 java.sql 中的类 java.sql.Date 和 java.util 中的类 java.util.Date 都匹配
In this case the full class name needs to be used
Precautions:
import is very different from #include in C++. C++ must #include to import other file content, but Java does not need it. import is just for convenience when writing code. import is more similar to namespace and using in C++
Knowledge point
The difference between import and package
package: "package", refers to: the package where the class is located
import: "introduce", refers to: import the class required in the class
If we want to use some code in the Java class library, we need to import it through import
2. Static import
Use import static to import static methods and fields in a package.
① Example:
import static java.lang.System.*; public class Test { public static void main(String[] args) { out.println("hello"); } }
In this way, System.out.println("hello"); can be written as out.println("hello");
② Another example:
import static java.lang.Math.*; public class TestDemo { public static void main(String[] args) { double x = 30; double y = 40; // 静态导入的方式写起来更方便一些. // double result = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); double result = sqrt(pow(x, 2) + pow(y, 2)); System.out.println(result); } }
Math. in the code can be removed.
3. Put the class into the package
basic rules:
Add a package statement at the top of the file to specify which package the code is in.
The package name needs to be as unique as possible, usually the reversed form of the company's domain name (eg com.xuexiao.demo1 ).
The package name must match the code path. For example, to create a package of com.xuexiao.demo1, there will be a corresponding path com/xuexiao/demo1 to store the code.
If a class has no package statement, the class is put into a default package.
Steps:
1) Create a new package in IDEA: Right-click src -> New -> Package
2) Enter the package name in the pop-up dialog box, such as com.xuexiao.demo1, and press Enter
3) Create a class in the package, right-click the package name -> New -> Class, and then enter the class name
4) At this point, you can see that the directory structure on our disk has been automatically created by IDEA
5) At the same time, we also see that at the top of the newly created Test.java file, a package statement appears
![]()
4. Package access control
We have seen public and private in a class. Members in private can only be used inside the class.
If a member does not contain the public and private keywords, then this member can be used by other classes inside the package, but cannot be used by classes outside the package.
Example:
The code below gives an example. Demo1 and Demo2 are in the same package, Test is in another package.
Demo1.java
package com.bili.demo; public class Demo1 { int value = 0; }
Demo2.java
package com.bili.demo; public class Demo2 { public static void Main(String[] args) { Demo1 demo = new Demo1(); System.out.println(demo.value); } } // 执行结果, 能够访问到 value 变量 10
Test.java
import com.bili.demo.Demo1; public class Test { public static void main(String[] args) { Demo1 demo = new Demo1(); System.out.println(demo.value); } } // 编译出错 Error:(6, 32) java: value在com.bili.demo.Demo1中不是公共的; 无法从外部程序包中对其进行访问
5. Common system packages
1. java.lang: Commonly used basic classes (String, Object) of the system, this package is automatically imported from JDK1.1.
2. java.lang.reflect: java reflection programming package;
3. java.net: Network programming development kit.
4. java.sql: A support package for database development.
5. java.util: is a tool package provided by java. (collection classes, etc.) very important
6. java.io: I/O programming development kit.