Java provides four kinds of access rights:
Public > Protected > Package access (no keywords) > private
Package: library Unit
For package access, all within a package can be accessed.
If you do not create a package when creating a class in Eclipse, the class will be created in the default package, which I have always been accustomed to, but this is a bad practice (= =!) Unrelated classes should be placed in different packages. Because if there are two classes in a package that have the same name, there will be conflicting results.
Put different classes into different packages, if you want to use other packages of classes, you will need to precede the name of the package name. For example, the ArrayList class will be written java.util.ArrayList when used in the Java.util directory. This is a lengthy name, so Java provides the Import keyword for importing the package.
Import java.util.ArrayList; You can then use the ArrayList directly in the file. If import java.util.*; All classes under Java.util will be imported.
There are packages and export packages that have an ingest package.
Packaging is implemented through the keyword package, which must be the first code of the file except for comments. The format is package PackageName, which is unique, so it is usually the reverse order of the domain name.
The Java interpreter runtime needs to find out the environment variable classpath, and we can write our own class library and then reference it. However, I did not do well for two hours, and temporarily gave up this,,, directly with Eclipse can be packaged into a jar (in the agency right-click on Export), and then in the import (right-built Properties-->java build Path-->add External JARs) can refer to its own class library.
The class can be imported through import xxx.myclass (XXX is the path). For a static method in a class, you can import static Xxx.myclass.staticMethod by importing it, and you can use Staticmethod () in a file without the class name. and import static xxx.myclass.*; All static methods can be imported. Note: only static methods can be used.
Access rights
For members in a class:
Public access is available to everyone.
When protected inherits, the subclass can access and have the package access rights.
Package access, a package that can be accessed.
Private only members within the class can access it.
For a class:
We all know that a file can have only one public class (or none), the class name is the same as the file name, so the other classes are package access rights. The class does not have private and protected (exceptions are in the inner class). A package access class is not visible to other packages.
Summary
There are two functions to provide access rights:
1. For users of the class, they will not be able to touch something they should not touch, while simplifying their understanding of the class.
2. Isolate and protect interfaces and implementations, changing the internal design of a class without significant impact on the users of the class.
Java Access Rights control