Access Control for Java class members (translated from Java tutorials)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.