What is the concept and usage of package in Java
Most people do not understand the knowledge points of this article "what is the concept and usage of package in Java", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the concept and usage of package in Java" article.
A little bit of eye contact
1. The declaration method of package:
Package package name [.package name 2.package name 3]
The import method of package 2 is as follows:
Import package package name. Class name
3 if there are hundreds of classes in a project, it will be troublesome to import one by one. In order to facilitate the import, you can use the form of "package name. *", for example:
Import java.io.*
The "*" here is a wildcard character that indicates "all" subclasses under the "java.io".
It is important to note that even if the format used is "package. *", it does not mean that all classes in this package are imported. The Java compiler is very smart. It will "import on demand". Java will not load any classes that are not needed in the code.
Second actual combat
1 the structure of the code
2 code
Person.java
Package demo.java.a; / / declare the methods in the pakagepublic class Person {public String talk () / / class {return "Person-- > > talk ()"; / / return a string}}
TestPackage.java
Package demo.java.b;import demo.java.a.Person;// imports the Person class from the demo.java.a package into this package class TestPackage {public static void main (String [] args) {/ / calls the method in demo.java.an and outputs System.out.println (new Person (). Talk ());}}
3 run
Person-> > talk ()
The above is the content of this article on "what is the concept and usage of package in Java". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.