Java basics: keyword static

static keyword

In a class, member variables declared with static are static member variables and also become class variables. The life cycle of a class variable is the same as that of a class, and it is valid during the execution of the entire application.

I want to emphasize here:

  • Static modified member variables and methods, subordinate to the class
  • Ordinary variables and methods are subordinate to objects
  • Static methods cannot call non-static members, compilation will report an error

The purpose of the static keyword

One sentence description is: it is convenient to call (method/variable) without creating an object.

Obviously, the method or variable modified by the static keyword does not need to rely on the object for access, as long as the class is loaded, it can be accessed through the class name.

Static can be used to modify the member methods of the class, the member variables of the class, and you can also write static code blocks to optimize program performance

static variable

The Java class provides two types of variables: static variables modified with the static keyword and instance variables not modified with the static keyword. A static variable belongs to a class, and there is only one copy in memory. As long as the class in which the static variable is located is loaded, the static variable will be allocated space, so it can be used. There are two ways to reference static variables, namely "class. static variable" and "object. static variable"

Instance variables belong to objects. Only after the object is created can the instance variables be allocated memory space and can be used. It has multiple copies in memory and can only be referenced in the form of "object. instance variable". Cannot define static variables inside member functions

Static variables are also called static variables, the difference between static variables and non-static variables:

  • Static variables are shared by all objects, there is only one copy in memory, and will be initialized when the class is first loaded

  • Non-static variables are owned by the object and are initialized when the object is created. There are multiple copies, and the copies owned by each object do not affect each other

static method

The static method has also become a static method. Since the static method does not depend on any object, it can be accessed directly. Therefore, for the static method, there is no this, because it is not attached to any object. Since there is no object, this can not be talked about. And because of this feature, non-static member variables and non-static methods of the class cannot be accessed in static methods, because both non-static member variables and non-static methods must rely on specific objects to be called.

Java provides static methods and non-static methods. The static method is a method of a class and can be called without creating an object, while a non-static method is a method of an object, which can be used only after the object is created.

The this and super keywords cannot be used in the static method, and the non-static method cannot be called. You can only access the static member variables and member methods of the belonging class, because when the static method is called, the object of this class may not have been created yet, even if it has been It has been created, and it is impossible to determine which object's method to call. Similarly, static methods cannot access non-static variables.

Singleton design pattern:

A very important use of static is to implement the singleton design pattern. The characteristic of the simple interest model is that the class can only have one instance. In order to achieve this function, the constructor of the class must be hidden, that is, the constructor is declared as private and a method of creating an object is provided. Because the constructed object is declared as private , The outside world cannot directly create this type of object, and can only obtain the object of the class through the method provided by the class. To achieve this purpose, the method of creating the object can only be declared as static

static block

The construction method is used to initialize the object. Static initialization block, used for the initialization operation of the class.

Non-staic members cannot be accessed directly in the static initialization block. These static code blocks will only be executed once

The role of the static block

The function of the static initialization block is to improve the performance of the program.

The static keyword in Java does not affect the scope of variables or methods. In Java, the only keywords that can affect access permissions are private, public, and protected (including package access permissions).

Although static member variables are independent of objects, it does not mean that they cannot be accessed through objects. All static methods and static variables can be accessed through objects (as long as the access rights are sufficient).

Static is not allowed to be used to modify local variables.

The meaning of using static and final in combination :

For variables, if you use static final modification, it means that once the value is assigned, it cannot be modified and can be accessed through the class name.

For a method, if the static final modification is used, it means that the method cannot be overridden and can be directly accessed through the class name.

Guess you like

Origin blog.csdn.net/PrisonJoker/article/details/105189194