final keyword --- Java

final keyword—Java

1. Decorate structure: class, variable, method

2. final is used to modify a class: this class cannot be inherited by other classes.

For example: String class, System class, StringBuffer class

3. final is used to modify the method: indicating that this method cannot be overridden

For example: getClass() in the Object class;

4. final is used to modify variables: the "variable" at this time is called a constant

  • final modified attribute: the position of assignment can be considered: explicit initialization, initialization in code block, initialization in constructor (only one of the above three places can be used for assignment) (not allowed in methods in classes)
  • Final modified local variables: especially when final modified parameters are used, it indicates that this parameter is a constant. When we call this method, we assign an actual parameter to the constant formal parameter. Once assigned, this formal parameter can only be used in the method body, but cannot be reassigned.

5. Notes

  • Final modified components do not support data type default assignment; (int defaults to 0, char defaults to '\u0000')

    Therefore, final int num is not allowed;
    it must be assigned directly, in ordinary code blocks, and in constructors;

  • The components of the static modifier are synonymous with the class

    Default values, direct assignments, static code blocks, ordinary code blocks, assignments in constructors, and assignments in methods are available.

6. Keywords and Permission Modifiers

Keywords and Permission Modifiers

Guess you like

Origin blog.csdn.net/E_chos/article/details/113346797