Java default Modifier

Source: Internet
Author: User

1. class)


Visibility modifier:Public-It can be seen in all classes that can be imported using import in other packages.

Default Value-There is no modifier. It can be seen in the class in the same package, and cannot be imported using import in other packages.

Modifier:Final-Final state class, indicating that the class cannot be inherited

Abstract-Abstract class, cannot create an object


2. variable)


There are two types of variables in Java: Method variables and field variables.

Method variables can only use one modifier:Final-It indicates a constant and cannot be modified.

The modifier of the field variable is as follows:

Visibility modifier:Public-Visible in any class

Protected-Visible in subclass or in the same package

Private-Visible only in this class and not in subclass

Default Value-Visible in the same package. The subclass is not in the same package, and the subclass is invisible.

Modifier:Static-Static variables shared by all instances of the class

Final-Constant, cannot be modified after Definition

* Transient-Tells the compiler that this variable does not need to be permanently saved during class Object serialization.

* Volatile-It is pointed out that there may be multiple threads to modify this variable, and the compiler is required to optimize it to ensure that the modification of this variable can be correctly processed.

These two tapes are not very common, but they are also Java modifiers, and I am not very clear about them.


3. method)


Visibility modifier:Public-Visible in any class

Protected-Visible in subclass or in the same package

Private-Visible only in this class and not in subclass

Default Value-Visible in the same package. The subclass is not in the same package, and the subclass is invisible.

Modifier:Static-Static Method: you do not need to create an instance of the class to access the static method.

Final-Common method. All subclasses cannot overwrite the method, but can be overloaded.

Abstract-Abstract methods, which are not implemented in abstract classes

Native-For local methods, see Java Native Interface (JNI)

Synchronized-In multiple threads, when the synchronized method is called, other methods cannot call this method.


4. interface)


Visibility modifier:Public-Visible to all classes

Default-Visible in the same package

The variables in the interface must always be defined as"Public static finalInterface Name ", but can not contain these modifiers, the compiler by default is like this

Because the interface cannot have a direct instance, that is, it cannot be modified (final) or accessed through the instance (static ).

Methods In the interface can only be usedPublicAndAbstractModifier

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.