static variables used in Java
Members in Java that are decorated statically are called static members or class members. It is owned by the entire class, not by an object, which is shared by all objects of the class. Static members can be accessed directly by using the class name or by using the object name. Of course, given the specificity of his role, it is more recommended to access it with a class name.
PS: Static members belong to the entire class, and when the system first uses the class, it allocates memory space until the class is unloaded for resource recycling
static methods for using static in JavaAs with static variables, we can also use the static modification method, called a static method or a class method. In fact, the main method we've been writing about is static methods.
1 static members can be called directly in a static method, but not non-static members directly. if you want to invoke a non-static variable in a static method, you can access the non-static variable by creating the object of the class and then through the object.
2, in the ordinary member method, you can directly access the same kind of non-static variables and static variables.
3. non-static methods cannot be called directly in static methods, and non-static methods need to be accessed through objects.
static initialization block used in Java in Java, data can be assigned by initializing blocks.
In the declaration of a class, you can include multiple initialization blocks, which are executed sequentially when you create an instance of the class. If you initialize a block with the static adornment, it is called a static initialization block.
It is important to note that static initialization blocks are only executed when the class is loaded and only once, while static initialization blocks can only assign values to static variables and cannot initialize normal member variables.
when the program runs, the static initialization block is executed first, then the normal initialization block is executed, and the construction method is finally executed. Because static initialization blocks are only executed once when the class is loaded, static initialization blocks are not executed when the object is created again.
Java Classes and objects