search
HomeJavajavaTutorialDetailed analysis of access modifiers in Java

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
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
What is the difference between JSON serialization and JDK serialization in storage?What is the difference between JSON serialization and JDK serialization in storage?Apr 19, 2025 pm 05:33 PM

Discussion on the differences between JSON serialization and JDK serialization in storage In the fields of programming and data storage, serialization is to convert objects into storable or transferable formats...

How to efficiently solve the coordinates of the intersection point when the projections of two line segments overlap in three-dimensional space?How to efficiently solve the coordinates of the intersection point when the projections of two line segments overlap in three-dimensional space?Apr 19, 2025 pm 05:30 PM

Solving the intersection coordinates of two line segments in three-dimensional space This article will explore how to solve the intersection coordinates of two line segments in three-dimensional space, especially when these two lines...

How to develop an HTTP request response monitoring software?How to develop an HTTP request response monitoring software?Apr 19, 2025 pm 05:27 PM

How to build HTTP request response monitoring software? This article will explore how to develop a software that can monitor relevant metrics in the client HTTP request and response process...

How to solve the problem that Flink cannot find Python task script when submitting PyFlink job to Yarn Application?How to solve the problem that Flink cannot find Python task script when submitting PyFlink job to Yarn Application?Apr 19, 2025 pm 05:21 PM

How to solve the problem that Flink cannot find Python task script when submitting PyFlink job to YarnApplication? When you are submitting PyFlink jobs to Yarn using Flink...

The output result of Java array is abnormal after expansion. What is the problem?The output result of Java array is abnormal after expansion. What is the problem?Apr 19, 2025 pm 05:18 PM

Java array expansion and strange output results This article will analyze a piece of Java code, which aims to achieve dynamic expansion of arrays, but during operation...

Docker Nginx deployment front-end project: How to resolve blank pages and proxy exceptions?Docker Nginx deployment front-end project: How to resolve blank pages and proxy exceptions?Apr 19, 2025 pm 05:15 PM

Blank pages and proxy exceptions encountered when deploying front-end projects with Docker Nginx. When using Docker and Nginx to deploy front-end and back-end projects, you often encounter some...

Spring Boot 3 Project: How to properly deploy external configuration files to Tomcat?Spring Boot 3 Project: How to properly deploy external configuration files to Tomcat?Apr 19, 2025 pm 05:12 PM

Deployment method of external configuration files of SpringBoot3 project In SpringBoot3 project development, we often need to configure the configuration file application.properties...

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use