Introduction to the Java language Tutorial (iii): Access modifiers for the Java language

Source: Internet
Author: User
Tags modifier modifiers

In the previous article, it was mentioned that Java classes need to be packaged and packaged for access control. In this article, you will make a brief introduction to the access modifier in the Java language.

A Java application has many classes, but some classes do not want to be used by other classes. There are data members and method members in each class, but not every data and method is allowed to be called in other classes. How can access control be achieved? You need to use the access permission modifier.

There are 4 types of access modifiers in the Java language, but there are only 3 keywords, which are referred to as default permissions in Java, or with package permissions, in this article (default), because they are not write access. The following permissions are introduced in the order of 4 from small to large.

1. Private Permissions Private

Private can modify data members, construct methods, method members, cannot modify classes (this refers to the external class, regardless of the inner class). Members that are decorated by private can only be used in the class that defines them and cannot be invoked in other classes.

2. Default permissions (Defaults)

class, data members, constructors, method members, can use default permissions, that is, no keywords are written. The default permissions are the same package permissions, and elements of the same package permission can only be invoked in the class in which they are defined, and in the class of the same package.

3. Protected permissions Protected

Protected can modify data members, construct methods, method members, and not modify classes (this refers to the external class, regardless of the inner class). Members that are decorated by protected can be called in the class that defines them, in the same package. If classes with different packages want to invoke them, then the class must be a subclass of the class that defines them.

4. Public permissions publicly

Public can modify classes, data members, constructor methods, method members. A member that is decorated by public can be invoked in any class, regardless of the same package or package, which is the most privileged modifier.

For the modifiers on the Java language, there are several issues to note:

1. Not every modifier can modify a class (that is, an external class), and only public and default can.

2. All modifiers can be decorated with data members, method members, and construction methods.

3. For the sake of code security, modifiers do not use the permissions as large as possible, but can be applied. For example, data members, if there is no special need, use private as much as possible.

4. Modifiers modify the permissions that are accessed.

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.