Home Java javaTutorial What are the modifiers in Java?

What are the modifiers in Java?

May 09, 2023 am 11:25 AM
java

The Java language defines 6 commonly used modifiers, including public, protected, private, abstract, static and final, and 5 less commonly used modifiers. The following is an introduction to these 11 Java modifiers:
1.public

Use objects: classes, interfaces, members

Introduction: No matter where the package it is in is defined, the class (interface, member) is accessible

2.private

Use object: member

Introduction: member can only be accessed in the class in which it is defined

3.static

Use objects: classes, methods, fields, initialization functions

Introduction: The inner class named static is a top-level class, which is not related to the members of the containing class. Static methods are class methods that are pointed to the class to which they belong rather than to instances of the class. A static field is a class field. No matter how many instances are created by the class in which the field is located, there is only one instance of the field that points to the class it belongs to rather than an instance of the class. The initialization function is executed when the class is loaded, not when the instance is created.

4.final

Used objects: classes, methods, fields, variables

Introduction: A class defined as final does not allow subclasses and cannot be overwritten (not Applied to dynamic queries), field values ​​are not allowed to be modified.

5.abstract

Used objects: classes, interfaces, methods

Introduction: Classes include unimplemented methods and cannot be instantiated. If it is an abstract method, the method body is empty, the implementation of the method is defined in the subclass, and the class containing an abstract method must be an abstract class

6.protected

Usage object: member

Introduction: A member can only be accessed in the package in which it is defined. If it is accessed in other packages, the class that implements this method must be a subclass of the class to which the member belongs.

7.native

Used object: member

Introduction: It is related to the operating platform. Its method is not defined when it is defined. The implementation of the method is implemented by an external library.

8.strictfp

Use objects: classes, methods

Introduction: All methods in classes modified by strictfp hide the strictfp modifier, and all floating points executed by the method Calculations comply with the IEEE 754 standard. All values, including intermediate results, must be represented as float or double types, without taking advantage of the additional precision or representation range provided by the native platform floating point format or hardware.

9.synchronized

Use object: method

Introduction: For a static method, the jvm locks the class it is in before execution; for a non-static class Method, lock a specific object instance before executing

.

10.volatile

Using objects: fields

Introduction: Because asynchronous threads can access fields, some optimization operations must not be applied to fields. volatile can sometimes replace synchronized.

11.transient

Using objects: fields

Introduction: Fields are not part of the persistent state of the object, and fields should not be strung together with the object.

The above is the detailed content of What are the modifiers in Java?. For more information, please follow other related articles on 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 Article Tags

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)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Square Root in Java

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Perfect Number in Java

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Random Number Generator in Java

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Armstrong Number in Java

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Weka in Java

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

Java Spring Interview Questions

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Smith Number in Java

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Break or return from Java 8 stream forEach?

See all articles