java: modifier

Java can use modifiers to modify methods and properties in a class. There are two main types of modifiers

classification

Access control modifiers: default, public, protected, private
Non-access control modifiers: final, abstract, static, synchronized

For example

public class ClassName {
    
    
   // ...
}
private boolean myFlag;
static final double weeks = 9.5;
protected static final int BOXWIDTH = 42;
public static void main(String[] arguments) {
    
    
   // 方法体
}

Access control modifier

In Java, you can use access control symbols to protect access to classes, variables, methods, and constructors. Java supports 4 different access rights.

  • default (ie default, write nothing): Visible in the same package without any modifiers. Use objects: classes, interfaces, variables, methods.
  • private: Visible in the same category. Use objects: variables, methods. Note: Can not modify the class (external class)
  • public: Visible to all classes. Use object: class, interface, variable, method
  • protected: Visible to classes and all subclasses in the same package. Use objects: variables, methods. Note: The class (external class) cannot be modified.

Non-access modifier

In order to achieve some other functions, Java also provides many non-access modifiers.

  • The static modifier is used to modify class methods and class variables.
  • The final modifier is used to modify classes, methods, and variables. Final modified classes cannot be inherited. Modified methods cannot be redefined by inherited classes. Modified variables are constants and cannot be modified.
  • The abstract modifier is used to create abstract classes and abstract methods.
  • The synchronized and volatile modifiers are mainly used for thread programming.

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/113484033