Access modifiers in Java detailed parsing _java

Source: Internet
Author: User
Tags control characters modifier modifiers volatile
1. The modifiers of the class are divided into two types: accessible and non-access control characters.

The accessibility control characters are: public class modifiers

Non-access control characters are: abstract class modifier abstract; final class modifier final

1 Common class modifiers public:There is only one accessible control character for classes in the Java language: public. The main class for each Java program must be a public class as a common tool for use by other classes and programs that should be defined as the publicly class.

2 abstract class modifier abstracts:Classes that are decorated with the abstract modifier are called abstract classes. An abstract class is a concept class that does not have a specific object for such a class. Such a class is the abstract class of the Java language.

3 Final class modifier final:When a class cannot have subclasses, the modifier final can be used to describe it as the final class. A class that is defined as final is usually a class that has fixed functions that are used to accomplish some standard function.

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

2. The control modifiers for a field are also divided into two categories: accessibility and non-access control characters.

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

There are 4 types of non-access control characters: Static field modifiers: statically; final field modifiers: final; volatile (shared) field modifiers: volatile; temporary domain modifiers: transient

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

2 Private access control character:A member variable (field) decorated with private can only be accessed by the class itself, not by any other class, including subclasses.

3 Protection Access control character protected:Member variables decorated with protected can be referenced by three kinds: ① the class itself, ② other classes in the same package as it does, ③ subclasses of that class in other packages. The primary role of using modifiers protected is to allow its subclasses in other packages to access specific properties of the parent class.

4 Private Protection access control character private protected:A member variable decorated with the modifier private protected can be accessed and referenced by the class itself or by two kinds of subclasses of the class.

5 static field modifier static:A member variable that is decorated with static is only a variable of a class, not a specific object, and the value of a static member variable is a public storage unit stored in the memory area of the class, rather than a memory interval that is saved in an object. When an object of any class accesses it, it takes the same data, and the object of any class modifies it to operate on the same memory unit.

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

7 volatile (shared) field modifier volatile:The volatile (shared) domain modifier volatile is used to illustrate 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 changed its value in the process of running the program. Therefore, we should pay attention to the change of 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 temporary domain modifier transient is used to define a transient variable. It is characterized by the use of modifiers transient limited temporary variables, the specified Java virtual machine to determine that the temporary variable is not a permanent state, to achieve the archiving function of different objects. Otherwise, all the variables in the class are part of the permanent state of the object, and the variables must be saved when the object is stored.

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

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

There are 5 types of non-access control characters: Abstract method control characters: Abstracts; static method control characters: Static; Final method control character: final; local method controller: Native; Synchronization method control character: synchronized

1 Abstract method Control symbol Abstract:A method that is decorated with the modifier abstract is called an abstract method. An abstract method is a method with only a method header, no method body, and an operation implementation.

2 static method controller static:A method decorated with a modifier static is called a static method. A static method is a class method that belongs to an entire class, and a method that is part of a specific class object without using a static modifier or a qualified method. Because the static method is part of the entire class, it cannot manipulate and process member variables belonging to an object, but only member variables that belong to the entire class, that is, the static method can handle only static fields.

3 Final method control character final:A method with the final modifier of a modifier is called the final method. The final method is a method that cannot be changed by functionality and internal statements, that is, the final method cannot be overloaded. In this way, the function and operation of the method are fixed, which prevents the subclass of the current class from defining the key methods of the parent class, and ensures the security and correctness of the program. All methods defined as private by the private modifier, and all methods contained in the final class (end Class), are considered the final method.

4 Local method control character native:Methods decorated with modifiers native are called local methods. In order to improve the running speed of the program, it is necessary to use other high-level language to write the method body of the program, then the method can be defined as the local method with modifier native to modify;

5 synchronization method control character synchronized:This modifier is primarily used for coordination and synchronization in multithreaded programs.

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.