Detailed parsing of access modifiers in Java

Source: Internet
Author: User
Tags control characters modifier modifiers volatile

1, the class of modifiers are divided into: access control and non-access control characters two.

The accessible control is: public class modifier

The non-access control characters are abstract class modifiers, and final class modifier final

1) public class modifier:There is only one accessible control for classes in the Java language: public is common. The main class for each Java program must be the public class that is used as a common tool for other classes and programs.

2) Abstract class modifier:A class that is decorated with an abstract modifier is called an abstract class. The so-called abstract class refers to a class of concepts that do not have specific objects. Such a class is the abstract class of the Java language.

3) Final class modifier final:When a class cannot have subclasses, the available modifier final describes it as the final class. Classes that are defined as final are usually classes that have a fixed effect and are used to accomplish some standard function.

4) Class default Access control character:If a class does not have an access control, it indicates that it has the default access control character attribute. At this point, the class can only be accessed or referenced by a class in the same package. This access feature is also known as package access.

2. The control modifiers for a domain are also categorized as: Access control and non-access control.

There are 4 accessible control characters: Public access control, private access control: private, Protected access control: protected; Private protected access control: Private protected

There are 4 types of non-access controls: Static field modifier: Statically, final domain modifier: final, volatile (shared) domain modifier: volatile, transient domain modifier: transient

1) Public access control:A field decorated with public is called a common domain. If the public domain belongs to a public class, it can be referenced by all other classes. Because the public modifier reduces the security of the run and the encapsulation of the data, you should generally reduce the use of the public domain.

2) Private access control:A member variable (domain) that is decorated with private can only be accessed by the class itself, and cannot be referenced by any other class (including subclasses).

3) Protect access control protected:Member variables modified with protected can be referred to by three species: ① the class itself, ② other classes in the same package, and ③ subclasses of that class in other packages. The primary purpose of using the modifier protected is to allow its subclasses in other packages to access the specific properties of the parent class.

4) Private protection access control private protected:Member variables decorated with the modifier private protected can be accessed and referenced by the class itself or subclass two of the class.

5) Static domain modifier static:A member variable that is decorated with static only belongs to a variable of the class, not to any specific object, and the value of the static member variable is the common storage unit of the memory area of the class, not the memory interval of the object being saved. When an object of any class accesses it, it is the same data, and when any object of the class modifies it, it also operates on the same memory unit.

6) Final domain modifier final:Final domain modifier final is used to define symbolic constants. The field of a class (member variable) if the modifier is final, its value is constant throughout the execution of the program.

7) volatile (shared) domain modifier volatile:volatile (shared) domain modifier volatile is used to indicate that this member variable may be controlled and modified by several threads. This means that the member variable may be affected by other programs or change its value while the program is running. Therefore, it is important to note the change in the value of this member variable in use. Typically volatile is used to decorate fields that accept external input.

8) Temporary domain modifier transient:The transient domain modifier transient is used to define a transient variable. Its characteristics are: with the modifier transient limited temporary variables, the specified Java virtual machine to determine that the temporary variable is not a permanent state, in order to achieve the different objects of the archiving function. Otherwise, all variables in the class are part of the permanent state of the object, and they must be saved when the object is stored.

3. The control modifiers of the method are also divided into two categories: accessible control and non-access control.

There are 4 accessible control characters: Public access control, private access control: private, Protected access control: protected; Private protected access control: Private protected

There are 5 types of non-access controls: Abstract, static method control: Static, Final method control: final; Local method control: Native; Synchronization method control: Synchronized

1) Abstract method control:The method that is modified with the modifier abstract is called an abstract method. An abstract method is a method that has only a method header, no method body, and an operation implementation.

2) static method controller static:The method of modifying static modifiers with modifiers is called a static method. A static method is a class method that belongs to the entire class, rather than a static decorated, qualified method that belongs to a specific class object. Because the static method belongs to the entire class, it cannot manipulate and process member variables that belong to an object, but only member variables that belong to the entire class, that is, the static method can handle only the static domain.

3) Final method control final:The method of final decoration with modifiers is called the final method. The final method is that the functionality and internal statements cannot be changed, i.e. the final method cannot be overloaded. In this way, the function and operation of this method are fixed, and the error definition of the parent class's key method is prevented by the subclass of the current class, which guarantees the security and correctness of the program. All methods that are qualified as private by the private modifier, and all methods contained in the final class (The final Class), are considered the final method.

4) Local method control native:A method decorated with a modifier native is called a local method. In order to improve the running speed of the program, it is necessary to use the method body of other high-level language writing program, then the method can be defined as local method with modifier native.

5) Synchronization method control synchronized:This modifier is primarily used for coordination and synchronization in a multithreaded coexistence program.

Detailed parsing of access modifiers in Java

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.