Java keywords: the use of static final abstract

1. The use of the static keyword

1. static: static
2. static can be used to modify, flexible, method, code block, inner class
3. Use static to modify attributes, static variables (or class variables)

3.1 Attributes, according to whether to use static modification, are divided into static attributes VS non-static attributes (instance variables).

Instance variables: We create multiple objects of the class, and each object independently has a set of non-static properties in the class. When modifying a non-static property in one of the objects, it will not cause the modification of the same property value in other objects.

Static variable: We create multiple objects of the class, and multiple objects share the same static variable. When a static variable is modified through an object, it will cause other objects to call this static variable, which is modified.

3.2 Other descriptions of static modification attributes:

  1. Static variables are loaded when the class is loaded. It can be called by "class.static variable"

  1. Static variables are loaded earlier than objects are created.

  1. Since the class will only be loaded once, there will only be one copy of the static variable in the memory, and it will be stored in the static field of the method area.

  1. Can it be called through a class or object

class variable instance variable

class yes no

object yes yes

3.3 Examples of static attributes: System.out; Math.PI;

4. Use static modification method: static method
  1. With the loading surface of the class, it can be called by "class.static method"

  1. Can it be called through a class or object

static method non-static method class

class yes no

object yes yes

  1. In static methods, only static methods or properties can be called

  1. In non-static methods, both non-static methods or properties can be called, and static methods or properties can also be called

5. Static attention points:
  1. In a static method, the this keyword and the super keyword cannot be used

  1. Regarding the use of static properties and static methods, everyone understands it from the perspective of life cycle.

6. During development, how to determine whether a property should be declared as static?
Attributes can be shared by multiple objects and will not vary from object to object.
During development, how to determine whether a method should be declared as static?
1. The method of manipulating static properties is usually set to static
2. The method in the class is customarily declared as static. For example: Math, Arrays, Collections

Second, the use of the final keyword

1.final: final
2. Final can be used to modify the structure: class, method, variable
3.final is used to modify a class: this class cannot be inherited by other classes.

For example: String class, System class, StringBuffer class

4.final is used to modify the method: it indicates that this method cannot be overridden.

For example: getClass() in the Object class;

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

a. Final modified attribute: The positions that can be considered for assignment are: explicit initialization, initialization in code blocks, and initialization in constructors

b. final modified local variable: especially when final modified parameter is 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.

6. Static final is used to modify attributes: global constants

3. The use of the abstract keyword

1. abstract: abstract
2. Abstract can be used to modify the structure: class, method
3. Abstract modified class: abstract class

This class cannot be instantiated. There must be a constructor in the abstract class, which is convenient to call when the subclass is instantiated. "Involved, the whole process of subclass object instantiation) v> During the development, the subclass of the abstract class will be provided, so that the subclass object instance to complete related operations

4.abstract modification method: abstract method

a. An abstract method has only a method declaration, no method body

b. A class containing abstract methods must be an abstract class. Conversely, abstract classes can have no abstract methods.

c. If the subclass rewrites all the abstract methods in the parent class, the subclass can be instantiated; if the subclass does not rewrite all the abstract methods in the parent class, then the subclass is also an abstract class. Need to use abstract modification

5. Notes on the use of abstract:

a. abstract cannot be used to modify: properties, constructors and other structures

b. abstract cannot be used to modify private methods, static methods, final methods, and final classes

Guess you like

Origin blog.csdn.net/weixin_44863237/article/details/128897965