Summary of common modifiers used in Java

Source: Internet
Author: User
Tags define local modifiers

Modifier Summary:

  One: public protected default Private

A decorated class, a decorated method, a decorated property, and a decorated code block.

class:
Top-level classes can only be decorated with public, and top-level classes cannot use private and protected adornments.
External classes can be modified by public or not written by default, and cannot be used with private and protected.
Inner classes can be static, available with protected and private adornments.

  Method:

The usual method can be modified by four access modifiers, and the construction method can be decorated with four access modifiers.
Abstract methods in abstract classes cannot be modified by private, and can be modified by the other three.
The methods in the interface can only be decorated with public (the default is public).

  Property:

Attributes can be decorated with four modifiers, and properties in an interface can only be decorated with public (the default is public).

code block:

Code blocks cannot be decorated with any modifiers (because the block is a property or a method, and the property and method have their own modifiers).


  constructs blocks of code: You can initialize non-static properties, and construct code blocks that take precedence over constructor execution and can be placed in constructors.

  Static blocks of code: You can initialize static properties, load when classes load, and not in constructors.


 Second: final:

  Decorated class: The final class, the final class cannot be inherited. The final class typically completes a class of standard functionality, such as math classes in the Java library, math, and so on.
  Modification Method: The final decorated method cannot be inherited, cannot be overridden (for example, wait () in object), but can be overloaded.
  constants: define local constants with final, define global constants with static final (or final static).

  three: static:
      modifier class: can decorate inner class
      Modifier property: Static can decorate any property.
      Modification method: cannot decorate an abstract method, and cannot modify a construction method.
      Cosmetic code block: the code block being decorated is called a static code block

Four: Abstract
decorated classes (abstract classes): Abstract classes cannot be instantiated. Abstract classes are used to be inherited.
Modification Method (abstract method): the method that can be modified is called abstract method, only the declaration of method, no implementation of method, end with semicolon.
cannot be modified: cannot modify attributes, private methods, constructors, static methods, final methods.

Summary of common modifiers used in Java

Related Article

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.