Home Java javaTutorial Detailed analysis of access modifiers in Java

Detailed analysis of access modifiers in Java

Jan 24, 2017 pm 03:43 PM

1. Class modifiers are divided into two types: accessible control characters and non-access control characters.

Accessible control symbols are: public class modifier public

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

1) Public class Modifier public: There is only one access control modifier for a class in the Java language: public, which is public. The main class of every Java program must be a public class. As a public tool for use by other classes and programs, it should be defined as a public class.

2) Abstract class modifier abstract: Any class modified with the abstract modifier is called an abstract class. The so-called abstract class refers to a conceptual class that has no concrete objects. Such a class is an abstract class of the Java language.

3) Final class modifier final: When a class cannot have subclasses, the modifier final can be used to indicate it as a final class. Classes defined as final are usually classes that have a fixed role and are used to complete certain standard functions.

4) Class default access control character: If a class does not have an access control character, it means that it has the default access control character. At this time, this class can only be accessed or referenced by classes in the same package. This access feature is also called package accessibility.

2 . Domain control modifiers are also divided into two categories: accessible control characters and non-access control characters.

There are 4 types of access control characters: public access control character: public; private access control character: private; protected access control character: protected; private protection access control character: private protected

non There are 4 types of access control modifiers: static domain modifier: static; final domain modifier: final; volatile (shared) domain modifier: volatile; temporary domain modifier: transient

1) Public access control Symbol public: The domain modified with public is called a public domain. If a public field belongs to a public class, it can be referenced by all other classes. Since the public modifier will reduce operational security and data encapsulation, the use of public fields should generally be reduced.

2) Private access control character private: Member variables (domains) modified with private can only be accessed by the class itself and cannot be referenced by any other class (including subclasses).

3) Protected access control character protected: Member variables modified with protected can be referenced by three categories: ① the class itself; ② other classes in the same package as it; ③ in other packages Subclasses of this class. The main purpose of using the protected modifier is to allow its subclasses in other packages to access specific properties of the parent class.

4) Private protected access control character private protected: Member variables modified with the modifier private protected can be accessed and referenced by the class itself or subclasses of the class.

5) Static domain modifier static: Member variables modified with static only belong to the variables of the class, and do not belong to any specific object. The value of the static member variable is a public storage stored in the memory area of ​​the class. unit, rather than being stored in the memory range of a certain object. When an object of any class accesses it, it gets the same data; when an object of any class modifies it, it also operates on the same memory unit.

6) Final domain modifier final: The final domain modifier final is used to define symbolic constants. If a field (member variable) of a class is specified by the modifier final, its value will remain unchanged 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. That is to say, during the running of the program, this member variable may be affected by other programs or change its value. Therefore, you should pay attention to the changes in the value of this member variable during use. Usually volatile is used to modify fields that accept external input.

8) Temporary domain modifier transient: The transient domain modifier transient is used to define a temporary variable. Its characteristics are: a temporary variable qualified with the modifier transient will specify the Java virtual machine to determine that the temporary variable does not belong to a permanent state, so as to realize the archiving function of different objects. Otherwise, all variables in the class are part of the object's permanent state and must be saved when the object is stored.

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

There are 4 types of access control characters: public access control character: public; private access control character: private; protected access control character: protected; private protection access control character: private protected

non There are 5 types of access control characters: abstract method control character: abstract; static method control character: static; final method control character: final; local method control character: native; synchronized method control character: synchronized

1) Abstract Method control operator abstract: A method modified with the abstract modifier is called an abstract method. An abstract method is a method with only a method header and no method body and operation implementation.

2) Static method control character static: A method modified with the modifier static is called a static method. Static methods are class methods that belong to the entire class; methods that are not modified or qualified with static are methods that belong to a specific class object. Since the static method belongs to the entire class, it cannot manipulate and process member variables belonging to an object, but can only process member variables belonging to the entire class. That is, the static method can only process the static domain.

3) Final method control character final: A method modified with the modifier final is called a final method. A final method is a method whose functionality and internal statements cannot be changed, i.e. a final method cannot be overloaded. In this way, the functions and operations of this method are fixed, preventing subclasses of the current class from incorrectly defining key methods of the parent class, and ensuring the safety and correctness of the program. All methods qualified as private by the private modifier, and all methods contained in a final class, are considered final methods.

4) Local method control character native: A method modified with the modifier native is called a local method. In order to improve the running speed of the program, the method body of the program needs to be written in other high-level languages. Then the method can be defined as a local method and modified with the modifier native;

5) Synchronized method control symbol synchronized: This modification Symbols are mainly used for coordination and synchronization in multi-threaded programs.

For more detailed analysis of access modifiers in Java and related articles, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

See all articles