Fully understand the static keyword

Preface: we often use the static keyword in Java, so many times we don't know when to use the static keyword, and what is the relationship between the internal logic used by the static keyword and the object? So today we'll talk about the static keyword and understand it in essence.

  • Static: static

  • static can be used to modify: attributes, methods, code blocks, internal classes

  • Use static to modify attributes: static variables (class variables)
    -Attributes are divided into static attributes vs non static attributes (instance variables) according to whether static modification is used
    ----------Instance variable: we have created multiple objects of the class, and each object independently has a set of non static attributes in the class. When modifying the non static property of one object, it will not result in the modification of the same property value in other objects.
    -----------Static variable: when we create multiple objects of a class, multiple objects share a static variable. When a static variable is modified through a, it will cause other objects to call the static variable. It is also modified.
    -Other descriptions of static modifier attribute:

      1.Static variables are loaded as the class is loaded. By "class"."Static variable"
      2.Static variables are loaded before objects are created
      3.Since the class will only be loaded once, only one static variable will exist in memory: in the static field of the method area
      4.See the table below
    
Class variableInstance variable
classyesno
objectyesyes
Examples of static attributes: System.out; Math.PI;
  • Using static to decorate methods: static methods

    ------1. It is loaded as the class is loaded, and can be called through "class. Static method"
    ------2.

Static methodNon static method
classyesno
objectyesyes

------3. In static methods, only static methods and attributes can be called; In non static methods, you can call either static methods and properties or non static methods and properties

  • static note:
    1. this keyword and super keyword cannot be used in static methods
    2. The use of static attributes and static methods can be understood from the life cycle

Analysis: because class loading precedes instantiated object loading (the object needs new constructor (), and the constructor has to load the class first), it can only be loaded through class Properties, classes Method to call; At the same time, it is executed after instantiating the object, so both static and non static can be called. This is to understand the use of static attributes and static methods from the life cycle.

  • During development, how to determine whether a method needs to be declared as static?
    ------>When attributes can be shared by multiple objects, they will not vary with different objects
    ------>Constants (final modifiers) in classes are also often declared as static

  • During development, how to determine whether a method should be declared as static?
    ------->The method for operating static properties is usually set to static
    ------->Methods in tool classes are traditionally declared as static. For example: Math, Arrays, Collections

  • Finally, understand the use of static through a simple code

public class StaticTest {
	public static void main(String[] args) {
		
		Chinese.nation = "China";
		
		
		Chinese c1 = new Chinese();
		c1.name = "Yao Ming";
		c1.age = 40;
		c1.nation = "CHN";
		
		Chinese c2 = new Chinese();
		c2.name = "Malone";
		c2.age = 30;
		c2.nation = "CHINA";
		
		System.out.println(c1.nation);
		
		//Compilation failed
//		Chinese.name = "Zhang Jike";
		
		
		c1.eat();
		
		Chinese.show();
		//Compilation failed
//		Chinese.eat();
//		Chinese.info();
	}
}
//Chinese
class Chinese{
	
	String name;
	int age;
	static String nation;
	
	
	public void eat(){
		System.out.println("Chinese people eat Chinese food");
		//Call non static structure
		this.info();
		System.out.println("name :" +name);
		//Call static structure
		walk();
		System.out.println("nation : " + nation);
	}
	
	public static void show(){
		System.out.println("I am a Chinese!");
		//Non static structures cannot be called
//		eat();
//		name = "Tom";
		//Static structures can be called
		System.out.println(Chinese.nation);
		walk();
	}
	
	public void info(){
		System.out.println("name :" + name +",age : " + age);
	}
	
	public static void walk(){
		
	}
}

Keywords: Java Back-end

Added by pkallberg21 on Thu, 20 Jan 2022 14:36:44 +0200