Thinking in Java: Chapter 6 Access Control

Source: Internet
Author: User

The maximum and minimum permissions are: public, protected, package access permission, and private.

6.1 package: Library Unit

However, when writing a Java source code file, this file is usually called a compilation unit. Each compilation unit must have a suffix. java, and there can be a public class in the compilation unit. The class name must be the same as the file name (including the case, but not the file suffix name. java ). Each compilation unit can have only one public class, otherwise the compiler will not accept it. If there are additional classes in the compilation unit, the class cannot be seen by the teachers and sisters outside the package, because they are not public classes, they are mainly used to provide support for public classes.

When you compile. for java files. each class of the Java file has an output file, and the name of the output file and. in the Java file, each class has the same name, but a suffix is added. class. Therefore, a large number of. class files are obtained after a small number of. java files are compiled. A Java runable program is a set of. class files packaged and compressed into a Java document file (jar, using the Java jar document builder. The Java interpreter is responsible for searching, loading, and interpreting these files.

As a class library designer, it is important to remember that the package and import keywords allow you to separate a single full name and namespace, so that no matter how many people use the Internet and Java to write classes, there will be no name conflict issues.

Installation Convention. The first part of the package name is the reverse-order Internet Domain Name of the class creator.

The second part is to break down the package name into a directory on your machine.

The Java interpreter runs as follows: First, find the environment variable classpath. Classpath contains one or more directories and is used to find the root directory of the. Class file. Starting from the root directory, the interpreter obtains the package name and replaces each sentence with a backslash to generate a path name from the classpath root (as a result, package Foo. bar. baz is changed to foo \ bar \ Baz or Foo/BAR/Baz, depending on the operating system ). The obtained path is connected to different items in the classpath. The Interpreter searches for the. Class file related to the class name you want to create in these directories.

Do not mistakenly think that Java always regards the current directory as one of the starting points for searching behavior. If a "." is missing from your classpath as one of the paths, Java will not find it.

Java does not have the Conditional compilation function of C. This function enables you to switch and generate different behaviors without changing the code of any program.

6.2 Java access permission Modifier

The only way to obtain access permissions for a member is:

1. Make the member public.

2. Assign package access permissions to members by placing other classes in the same package without an access modifier. Other classes in the package can access this member.

3. The inherited class can access both public and protected members. Only when both classes are in the same package can they access the package access permission members. But now you don't have to worry about inheritance and protected.

4. Provide the accessors and mutator methods (also known as the get and set methods) to read and change values.

6.3 Interfaces and Implementations

To clean up, a public member may be placed at the beginning, followed by the form of protected, package access permission, and private member creation class. The advantage of this is that the class users can start reading from the beginning. First, they can read the most important part of the class and stop reading when they encounter non-public members who are internal implementation details.

Classes cannot be private or protected. However, an internal class can be private or protected, but it was a special case at that time.

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.