<Java>< modifiers >

Source: Internet
Author: User

Access modifiers

Access modifiers

Name

Description

Note

Public

can be accessed by any class

Protected

can be accessed by all classes in the same package

can be accessed by all sub-classes

Subclasses are not accessible in the same package

Private

can only be accessed by methods of the current class

Default (no access modifier)

can be accessed by all classes in the same package

if the subclass is not in the same package, you cannot ask

Variable modifiers
    • Java has no global variables, only method variables, instance variables (non-static variables in the Class), class variables (static variables in the Class).
    • , and the other two variables have the same access modifier as the previous table.
    • When declaring an instance variable, if the initial value is not assigned, then . The
    • can initialize more complex instance variables through an instance variable initializer. instance variable initialization block is a block of statements contained with {}, which runs after the constructor of the class is invoked, before the constructor is run after the parent class constructor. The
    • class variable (static variable) can also be initialized with a class-variable initializer, which is a block of statements contained in static{} and may only be initialized once.

Mainly because the variable can come through other variables, use it for performance problems

TD Width= "348" >

modifier

Name

description

remarks

Static

Static variables (also known as class variables, others called instance variables)

can be shared by all instances of the class.

Does not need to create an instance of the class to access static variables

Final

constants, values can only be assigned once, cannot be changed

Take care not to use const, although it is the same as the Const keyword in C, C + +, and can be used with static to avoid maintaining a copy of each instance of the class

P>transient

tells the compiler that when a class object serialization, this variable does not need to persist

Volatile

< p> indicates that there may be multiple threads modifying this variable, requiring the compiler to optimize to ensure that modifications to this variable are handled correctly

 

 

    • volatile: In the current Java memory model, threads can store variables in local memory (such as a machine's register), rather than read and write directly in main storage. This can cause a thread to modify the value of one variable in main memory, while another thread continues to use its copy of the variable value in the register, resulting in inconsistent data. Declaring a variable as volatile (unstable) will indicate that the JVM is unstable, and each time it is used it is read in main memory. Also, when a member variable changes, forcing the thread to write the change back to the shared memory. So at any moment, two different threads always see the same value for a member variable.
Method modifiers
    • Access modifier + modifier + return type + method name (parameter list) + throws violation list
    • Class constructor methods cannot have modifiers, return types, and throws clauses.
    • When a class constructor method is called, it first constructs the parent class and then runs the initializer for the instance variable and the static variable, and then runs the constructor itself .

Modifier

Name

Description

Note

Static

Static methods (also known as class methods, others called instance methods)

Provide services that do not depend on class instances

You do not need to create an instance of a class to access a static method

Final

Prevents any subclasses from overriding the method (can be overloaded)

Be careful not to use const, although it has the same meaning as the Const keyword in C, C + +

Can be used with static to avoid maintaining a copy of each instance of the class

Abstract

Abstract methods, methods that have been declared in a class and not implemented

You cannot declare a static method, a final method, or a constructor method of a class as abstract

Native

The method defined with this modifier is not implemented in the class, and in most cases the implementation of the method is written in C and C + +.

See Sun's Java Native Interface (JNI), which provides the runtime to load an implementation of a native method and associate it with a Java class function

Synchronized

Multi-threaded support

When one of these methods is called, no other thread can call the method, and the other synchronized method cannot call the method until the method returns

Interface modifiers
    • Access modifier Interface Interface Name extends interface list
    • Access modifiers

      Name

      Description

      Public

      All

      No access modifier (default)

      In the same package

<Java>< modifiers >

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.