Java Access Rights control

Source: Internet
Author: User
Tags contains root directory

Access control (or hide specific implementations) is related to "improper implementation of the original"

The level of the access control character, from maximum to minimum permissions: public, Protected, package access (no keywords), private

1. Package: Library Unit Package

The package contains a set of classes that are organized together under a single namespace.

Import Keyword: If you want to import a single class import Java.util.ArrayList

You want to import all of the classes that are imported java.util.*

Package mechanism: In order to solve the name space, name conflict

This file is often referred to as a compilation unit when writing a Java source code file. Each compilation unit must have a suffix name. Java

There can be a (only one) public class within the compilation unit, and the class must have the same name as the file. If in the compilation unit

There are other classes that are not visible outside the package because they are not public classes and they provide support for the public class.

1. Code Organization

Package statement: Must be the first program code in a file other than comments

The package and import keywords separate the single global namespace so that no matter how many people use the Internet and Java to start writing classes, there is no name conflict problem.

2. Create a unique package name

By convention: The first part of the package name is the reverse-order Internet domain name of the creator of the class.

Java Interpreter run Process: First, find the environment variable CLASSPATH. Classpath contains one or more directories that are used as the root directory to find. class files. Starting at the root, the interpreter takes the name of the package and replaces each period with a backslash to produce a path name from the Classpath root. The resulting path name is connected to various items in the classpath, and the interpreter finds the. class file in these directories that relates to the name of the class you are creating.

Conflict: A conflict occurs when two classes with the same name are imported together in a "*" form. To resolve the conflict, you must explicitly indicate the full path of the calling class.

3. Custom Tool Class

You can create your own ToolPak to reduce or eliminate duplicate code.

4. Change behavior with Import

A class that writes the same name but behaves differently in a different package can change the behavior of the class by importing different classes.

5. Advice on the use of packages

Whenever a package is created, the directory structure is implicitly specified when the name of the package is given.

2.Java access modifier

1. Package access rights

The default access permission does not have any keywords, usually refers to package access rights.

Package access allows all related classes in the package to be grouped so that they interact easily with each other.

The only way to get access to a member of a class:

1. Make the member public

2. Assign a package access to a member by modifying the word without access control and placing other classes in the same package.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

3. Inherits access to public and protected members, and only the inherited two classes are in the same package, it can access the members of the package access rights.

4. Provide accessors and variants (get and Set methods)

2.public: Interface access rights

Using the keyword public means that the member immediately following public declares that he or she is available to everyone.

Default package: is in the same directory and does not set any package names for itself. Java automatically considers such files to be subordinate to the

The default package for the directory, so they provide package access to all other files in the directory.

3.private: You can't access

Private: No other class can access this member except the class that contains the member.

4.protected: Inheriting access rights

Subclass inherits the parent class, and subclasses can access the parent class protected member

5. Scope public,private,protected, and the difference when not written:

The current class the same package different package descendants class other package

Public√√√√

Protected√√√x

Friendly√√xx

Private√xxx

3. Access rights for classes

1. Each compilation unit can have only one public class

The name of the 2.public class must match exactly the file name that contains the compilation unit

3. It is also possible to have a complete without public class in the compilation unit, in which case the file can be named arbitrarily.

Note: Access to class: Only two: package access or public

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.