From http://www.cnblogs.com/ggjucheng/archive/2012/11/28/2793339.html
The access level modifier determines whether other classes can use its specific fields or call specific methods. There are two levels of access control:
Top-level-public, or private in the package (no explicit modifier)
Member level-public, private, protected, or private in the package (no explicit modifier)
A class can be modified using public. In this case, the class can be accessed by other classes anywhere. If the class has no modifier (default, private package), it can only be visible in its own package (the package is a group of related classes and will be learned later)
At the member level, you can use a public modifier or a non-modifier (private in the package), which has the same meaning as the top-level class. For members, there are two additional access modifiers: Private and protected. the private modifier indicates that the member can only be accessed by its own class, and the protected modifier indicates that the member can only be accessed in its own package (such as private in the package). However, it can be accessed by its subclass in other packages.
The following table shows the member access permissions for each modifier.
Access levels
Modifier |
Class |
Package |
Subclass |
World |
Public |
Y |
Y |
Y |
Y |
Protected |
Y |
Y |
Y |
N |
No Modifier |
Y |
Y |
N |
N |
Private |
Y |
N |
N |
N |
The first column indicates whether the class itself has access permissions to members at this access level. As you can see, a class can always access its members. The second column only indicates whether the class of the same package (whether inherited or not) has the permission to access members, and the third column indicates whether the class outside the package has the permission to access the member. The fourth column indicates whether all classes have the permission to access this member.
The access level affects you in two aspects. First, when you use classes from other sources, such as classes on the Java platform, the access level determines which members of these classes can be used by your own classes. Second, when you write a class, you need to determine the access level of each member variable and each method.
Let's take a look at how the class set and access level affect visibility. In this example, there are four main categories and their relationships.
Example of class and package access level
The following table shows the members of the Alpha Class in each access modifier and whether they are visible to other classes.
Visibility
Modifier |
Alpha |
Beta |
Alphasub |
Gamma |
Public |
Y |
Y |
Y |
Y |
Protected |
Y |
Y |
Y |
N |
No Modifier |
Y |
Y |
N |
N |
Private |
Y |
N |
N |
N |
How to select an access level:
If otherProgramWhen using your class, make sure that abuse does not cause errors. The access level can help you do this.
It is reasonable for specific members to use the strictest access level. Try to use private unless you have a good reason.
Except constants, other fields are not public (public fields are used in many examples in this tutorial. This may be helpful for concise descriptions, but is not recommended for production.Code.) Public fields often lead to a specific implementation that limits your flexibility to change your code.