Java learning road-the use of static keyword

Java learning road-the use of static keyword

Overview

static It is a static modifier of a Java program. What is a static modifier?

As we all know, any variable or code in the program is automatically allocated by the system to store memory at compile time, and the so-called static means that the allocated memory will always exist after compilation, and the memory will not be released until the program runs and exits. This space. That is, as long as the program is running, this memory will always exist.

static It means "global" or "static", used to modify member variables and member methods, and can also form static static code blocks, but there is no concept of global variables in the Java language.

One, static attribute

According to whether to classify class member variables statically, there are two types:

  • One is the variable modified by static, called static variable or class variable;
  • The other is a variable that is not modified by static, called an instance variable.

The differences between the two are:

  • JVM allocates memory only once for static, and there is only one copy of static variables in memory (memory saving);
  • Complete the memory allocation of static variables in the process of loading the class;
  • The loading of static variables precedes the loading of instances;
  • Every time an instance is created, memory will be allocated to the instance variable, and the instance variables between the instances do not affect each other;

Timing of use : Under normal circumstances, we will belong to a class and will not set variables that change with the instance object to static variables.

access permission

Call object Class attribute Instance attributes
class
Instance object
public class Demo {
    
    
    public static void main(String[] args) {
    
    
//        直接通过 Man 类调用 gender 属性
        System.out.println(Man.gender);

//        实例属性
        Man m1 = new Man();
        m1.name = "张三";
        Man m2 = new Man();
        m2.name = "李四";
        System.out.println(m1.name);
        System.out.println(m2.name);
//        通过实例对象调用 gender 属性
        System.out.println(m1.gender);

//        通过 m1 修改了类属性,那么 m2 输出的类属性也会变化
        m1.gender = "女";
        System.out.println(m2.gender);
    }
}

class Man {
    
    
    String name;
    static String gender="男";
}

Two, static method

Static methods can be called directly by the class name, and any instance can also be called.

Therefore , the this and super keywords cannot be used in static methods, and the instance variables and instance methods of the class cannot be directly accessed (that is, member variables and member methods without static), and only static member variables and member methods of the class can be accessed. Because instance members are associated with specific objects!

Because the static method is independent of any instance, the static method must be implemented, not an abstract abstract.

Timing of use : Generally, we set the method of modifying the class attribute and the method of the tool class as the class method.

Call permission

Call object Class method Instance method
class
Instance object

Three, static code block

A static code block is also called a static code block. It is a static statement block in a class that is independent of class members. There can be multiple, and the position can be placed at will. It is not in any method body. Ordinary code blocks are run when an instance is created, in which you can access class attributes and methods, as well as instance attributes and methods.

JVM will execute these static code blocks when loading a class. If there are multiple static code blocks, the JVM will execute them in the order in which they appear in the class, and each code block will only be executed once.

Object Access class attributes and class methods Access instance properties and instance methods
static code block
Normal code block
public class Demo {
    
    
    static {
    
    
        System.out.println("Hello!");
    }

    public static void main(String[] args) {
    
    
        System.out.println(new Man());;
    }

    static {
    
    
        System.out.println("Bye!");
    }
}

class Man {
    
    
    {
    
    
        System.out.println("Nice to meet you.");
        System.out.println(this.getClass());
    }
}
// Hello!
// Bye!
// Nice to meet you.
// class Man

Through the above example, we can find that the static code block is automatically executed first in order.

If you need to initialize static variables through calculations, we can process them by declaring a static code block, which is executed only once when the class is loaded. If we need to manipulate the instance object when creating the instance, then we can use ordinary code blocks.

Guess you like

Origin blog.csdn.net/qq_43580193/article/details/112592830