Zero-based learning Java 07

Table of contents

static keyword

final keyword

code block


static keyword

  • static (static keyword), you can modify member variables and member methods
  • The characteristics of the static keyword: 1. It is loaded with the loading of the class; 2. It exists prior to the object; 3. It is shared by all objects of the class (the condition for judging whether to use the static keyword static); 4. It can be called by the class name (Recommended Use)
  • Static method : The static method is also called a static method. Since the static method can be accessed without relying on any object, there is no use of the this keyword for the static method, because the this keyword is attached to the object. In the static Non-static members of the class cannot be accessed in methods, but static member methods or static variables can be accessed in non-static member methods
  • Static variables : static variables are also called static variables or class variables. The difference between static variables and non-static variables is that static variables are shared by all objects and have only one copy in memory. Initialization; non-static variables are owned by the object and are initialized when the object is created. There are multiple copies in memory, and the copies owned by each object do not affect each other. The initialization order of static member variables is carried out in the order of definition
  • Note:
  • The static keyword in Java does not affect the scope of variables or methods. Only access modifiers can affect access rights in Java.
  • Although static member variables are independent of the object, they can still be accessed through the object (as long as the access rights are sufficient)
  • static is not allowed to modify local variables
  • It can be understood that the static element has no relationship with the object, it belongs to the class
  • This and super keywords cannot be used in static elements
  • Static variables are stored in the static area in the method area

 

final keyword

  • The final keyword can be used to modify classes, methods and variables
  • Modified class : When a class is modified with final, it indicates that the class cannot be inherited. The member variables in the final class are not modified by default and can be set to final as needed, but all methods in the final class are modified by default.
  • Modification method : For the rewriting problem, when a method in the parent class is final modified, it indicates that the method in the parent class cannot be overridden by the subclass, that is, the subclass is prohibited from rewriting this method (the main purpose is prevent the contents of the method from being modified)
  • Note : The premise of rewriting is that the subclass can inherit this method from the parent class. If the final modified method in the parent class is also modified by private, there will be no conflict between rewriting and final at this time, because the subclass does not have any Inherit this method, this method is privatized, since there is no rewriting of inheritance, final has no effect, so when a method in the parent class has both private and final modifiers, in the subclass The same declared method can still appear, because this is considered to redefine the new method in the subclass
  • Modified variables : Variables modified by final become constants. Constants cannot be reassigned, read-only and not writable. When defining constants in Java, static modification is generally added, because constants are unchanged, and all objects have the same , if you always keep the same new thing, memory will be wasted
    Note : member variables defined as final must be initialized when the object is constructed, and cannot be modified later
  • When final modifies the basic data type, the value cannot be changed, and when final modifies the reference type data, the address value cannot be changed, but the content of the object can be changed
  • Final modified instance variables must be manually assigned and cannot adopt the system default value
  • The final method in the parent class can be inherited by subclasses, but cannot be overridden by subclasses
  • The object pointed to by the final modified reference cannot be reclaimed by the garbage collector

Note : When a variable is modified by final, the variable becomes a constant. Since it is a constant, what it stores in memory is only a value, which has nothing to do with the previous variable memory. That is, when the variable disappears, the constant It will not disappear, and the value is still being calculated, so if you want a certain data not to disappear due to the disappearance of the variable, modify it as a constant 

 

code block

The code enclosed by { } in Java is called a code block, which can be divided into: local code block, construction code block, static code block, synchronization code block according to its position and declaration

  • Local code block : It is also called ordinary code block, which appears in the method and limits the variable life cycle. It is mainly used to solve the problem of repeated variable names in the current method. If you want to use the same variable name multiple times in a method without affecting each other, you can put the variable in different local code blocks, because the variable life cycle in the local code block is limited to the code block
  • Construction code block : Appears outside the method in the class. The same code in multiple construction methods is stored together and executed every time the construction is called. As long as the object is created, the construction code block will be executed. The main function is to initialize the object
  • Static code block : Appears outside the method in the class, adds the static modifier, is executed first, and only executes once for multiple objects of a class, its main function is to initialize the class, and execute as the class is loaded , has nothing to do with creating or not creating an object
  • Synchronized code block : Appears in the method and is modified with the synchronized keyword. In a multi-threaded environment, read and write operations on shared data need to be mutually exclusive, otherwise it will lead to data inconsistency

The execution order of the above code blocks :

Static code block --> Construction code block --> Construction method --> Partial code block
 

Guess you like

Origin blog.csdn.net/timberman666/article/details/130142238