Java modifier Overview

Source: Internet
Author: User

Technorati MARK: java. The overall usage of modifiers is described as follows:

Modifier

Modified parts

Description

Abstract

Class
Interface
Method

Initialization is not allowed. methods that have not been implemented are included.
All interfaces are abstract. whether to add abstract
The constructor cannot be modified. Only common methods can be modified. Internal classes are abstract.

Final

Class
Method
Field
Variable

Cannot be inherited, that is, no subclass
It cannot be overwritten and can be searched dynamically.
Its value cannot be modified. static final fields definition constant
The value variable cannot be changed to a local variable)

Native

Method

Platform method. Without the modifier, only the common method is modified.

None (package)

Class
Interface
Member

Only the lower class of the same package can access
Only the lower class of the same package can access
Only the lower class of the same package can access

Private

Member

Can only be accessed by the current class

Protected

Member

It can be accessed by a quilt class or a class under the same package.

Public

Class
Interface
Member

Accessible to all classes
Accessible to all classes
Accessible to all classes

Strictfp

Class
Method

All methods in the modified class must follow strict floating point operations.
All float and double expressions in this method strictly abide by the restrictions of FP-strict and comply with the IEEE-754 specifications. This is rarely used.

Static

Class
Method
Field
Initializer

However, an internal class becomes an external class.
You can call this method directly by using the method name.
When modifying a field, the field is not an instance of the class.
This class starts to run when it is loaded, instead of waiting until it is created.

Synchronized

Method

For static methods, you need to add a lock for the class before executing the method;

For non-static methods, you need to add a lock for the class instance before execution.

Transient

Field

No persistence is required during Object serialization.

Volatile

Field

It can be accessed by asynchronous threads and is rarely used.

Usage principles
  • The access modifier of a method can only be public, protected, or private.
  • You cannot use abstract and final to modify a class at the same time.
  • Abstract methods cannot be defined as private, static, final, native, strictfp, or synchronized.
  • You cannot use both native and strictfp to modify a method.
  • Abstract and native modifier methods do not have a method body
    • Abstract void method();
    • Native void method();
  • When a class contains the abstract method, this class must be defined as abstract

The following lists the types of modifiers that can be modified.

Modifier

Class

Variable

Method

Constructor

Free Block

Public

Yes

Yes

Yes

Yes

No

Protected

No

Yes

Yes

Yes

No

None or package or default

Yes

Yes

Yes

Yes

Yes

Private

No

Yes

Yes

Yes

No

Final

Yes

Yes

Yes

No

No

Abstract

Yes

No

Yes

No

No

Static

No

Yes

Yes

No

Yes

Native

No

No

Yes

No

No

Transient

No

Yes

No

No

No

Volatile

No

Yes

No

No

No

Synchronized

No

No

Yes

No

Yes

Strictfp

Yes

No

Yes

Yes

No

 

I have also seen some good summaries on the network, which are listed here for your reference,

I,Class Access Control Modifier(Non-internal class)

Modifier

Public

Default Modifier

Function

Accessible anywhere

Package access permission: the package can be accessed, but cannot be accessed outside the package.

II,The feature modifiers of the class are mutually exclusive and compatible.(Non-internal class)

Modifier

Final

Abstract

Strictfp

Function

Class cannot be derived

Class to be inherited

Class adopts strictly precise floating-point operations

Final

--

No

Yes

Abstract

No

--

Yes

Strictfp

Yes

Yes

--

III,Class member access control Modifier

Modifier

Scope

Public

Protected

Default Modifier

Private

Out-of-Package

Yes

No

No

No

Subclass

Yes

Yes

No

No

Package

Yes

Yes

Yes

No

Class

Yes

Yes

Yes

Yes

IV,Class member variable property modifier

Modifier

Final

Static

Volatile

Transient

Function

Unchangeable

Single Copy

Shared Memory

Instantaneous

Final

--

Yes

No

Yes

Static

Yes

--

Yes

Yes

Volatile

No

Yes

--

Yes

Transient

Yes

Yes

Yes

--

V, Class member method property modifier

Modifier

Final

Abstract

Static

Native

Synchronized

Strictfp

Constructor

No

No

No

No

No

Yes

Final

--

No

Yes

Yes

Yes

Yes

Abstract

No

--

No

No

No

No

Static

Yes

No

--

Yes

Yes

Yes

Native

Yes

No

Yes

--

Yes

Yes

Synchronized

Yes

No

Yes

Yes

--

No

Strictfp

Yes

No

Yes

Yes

No

--

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.