[Java] access permission Control

Source: Internet
Author: User

1. Package

The package contains a group of classes that are organized together in a single namespace. For example, there is a tool library in the Java standard release, which is organized under the java. util namespace. Java. util has an ArrayList class, which can be used with its full name. For example:

Public class Test {
Public static void main (String [] args ){
Java. util. ArrayList list = new java. util. ArrayList ();
}
}

This statement makes the program very lengthy, so you can use the import keyword instead. If you want to import a single class, you can name the class in the import Statement, for example:

Import java. util. ArrayList;
Public class Test {
Public static void main (String [] args ){
ArrayList list = new ArrayList ();
}
}

To import all the classes, you only need to use "*", for example:
1

Import java. util .*;

The reason for using import is to provide a mechanism for managing namespaces. Due to potential conflicts between class names, it is very important to fully control the namespace in Java and create a unique combination of identifiers for each class.

When writing a Java source code file, this file is usually called a compilation unit and 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 suffix name ), each compilation unit can have only one public class.

When you compile. when a java file is created, each class in the file has an output file. The name of the output file is the same as that of each class in the file, but the extension is added. class. A Java executable program is a set of. class files that can be packaged and compressed into a Java document file (JAR file. The Java interpreter is responsible for searching, loading, and interpreting these files.

A class library is actually a group of class files. Each file has a public class and any number of non-public classes. Therefore, each file has a component, if you want these components to belong to the same group, you can use the keyword package, and it must be the first program code in the file except comments, for example:

Package access. mypackage;
Public class MyClass {
}

If you want to use MyClass, you must use the import keyword to make the access name available. For example:

Import access. mypackage .*;
Public class Test {
Public static void main (String [] args ){
MyClass m = new MyClass ();
}
}


2. access permission Modification

Java access permission modifiers include public, protected, and private. Before each member in the class is defined, each access permission modifier only controls access to the specific definition it modifies. If no access permission modifier is provided, it means that it is the package access permission. Package access permission means that all other classes in the current package have access permissions to that member, but this member is private for all classes outside the package.

The class controls which code has the right to access its own members. The way to gain access to a member is either to make the member public, you can either place other classes in the same package without the access permission modifier and grant the access permission. In addition, the inherited classes can access public members or protected members.

Using the public keyword means that the Members that follow the public statement are available to everyone. For example:

Class Test {
Public void SayHello (){
System. out. println ("Hello World! ");
}
}
Public class Main {
Public static void main (String [] args ){
Test t = new Test ();
T. SayHello ();
}
}

The private keyword indicates that, except for the class containing this member, no other class can access this member. For example:

Class Test {
Private Test (){}
Static Test makeTest (){
Return new Test ();
}
}
Public class Main {
Public static void main (String [] args ){
Test t = Test. makeTest ();
}
}

The keyword protected is used to process the concept of inheritance. An existing class can be used through inheritance. to inherit from the existing class, an existing class exntends needs to be declared. For example:

Class SubTest extends Test {
}

If the creator of the base class wants a specific member to grant its access permission to the derived class instead of all classes, it needs protected. protected also provides the package access permission, that is, other classes in the same package can access the protected element, for example:

Class Test {
Protected void SayHello (){
System. out. println ("Hello World! ");
}
}
Public class Main {
Public static void main (String [] args ){
Test t = new Test ();
T. SayHello ();
}
}


3. Class Access Permissions

In Java, the access permission modifier can also be used to determine which classes in the library are available to users of the library. If you want a class to be available, you can use the public keyword to define the entire class. For example:


Public class Test {
}

However, there are some additional restrictions. Each compilation unit can have only one public class, which means that each compilation unit has a single public interface and is represented by a public class. The public class name must match the file name containing the compilation unit, including the case. In addition, the compilation unit does not include a public class. classes without permission modifiers have the package access permission, that is, the class can only be used in the package.

You must note that the class cannot be private or protected. Therefore, you have two options for the class access permission: Package access permission or public. If you do not want anyone else to have access to the class, you can specify all constructors as private to prevent anyone from creating the Class Object. There is only one exception, it is created within the static member of the class.

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.