static keyword, hungry-man, lazy-man display in single-case mode

I.   Use of the static keyword

1.static: static

  2.static can be used to decorate attributes, methods, code blocks, internal classes

  3. Modify attributes with static: static variables

3.1 Attribute: Static variables (class variables) by whether or not they are modified with statics   vs   Non-static variable (instance variable)
     Static variables: We create multiple objects of a class that share the same static variable. When a static variable is modified through an object, it results in
             This static variable was modified when other objects called it.
     Instance variable: We create multiple objects of a class, each of which has its own set of non-static properties within the class. When modifying one of the objects
             Non-static attributes do not result in changes to the same attribute values in other objects.
     3.2 Additional descriptions of static-modified attributes:
         1. Static variables are loaded as classes are loaded. Calls can be made through Class Static Variable
         (2) Static variables are loaded earlier than objects are created.
         (3) Since classes are only loaded once, there will only be one static variable in memory: a static field with a method area.
         (4) Class variables can be invoked with both class and instance objects, and instance variables can only be invoked with instance variables

  4. Use static modifier: static method

(1) Loading as classes are loaded can be invoked by "Class. Static methods"
     (2) In static methods, only static methods or properties can be invoked.
       In a non-static method, you can call either a non-static method or property or a static method or property.

  5. Note:

(1) In a static method, neither the this keyword nor the super keyword can be used, they are both current objects.. Meaning, while a static method can be called by a class  
(2) Classes are loaded with static variables and static methods, and objects are created before such statements as this and super are made. When it comes to calls, think in the order of life cycle

  6. In development, how do I determine if an attribute is to be declared static?

Attributes can be shared by multiple objects, and do not vary from object to object.
(2) It is also common to see that some constants in a class are also modified with static s. For example, Math.PI, allows PIs to be called directly from the Math class and for each circle Π It's all this value, and it's common, but it's also modified with final because it's a constant and won't be modified
(3) The method for manipulating static attributes is usually set to static.
(4) Methods in tool classes are customarily declared static. Examples: Math, Arrays.Collections

public class StaticTest {
	public static void main(String[] args) {

		Chinese.nation = "China";

		Chinese c1 = new Chinese();
		c1.name = "Haiyu";
		c1.age = 18;
		// c1.nation = "CHN";

		Chinese c2 = new Chinese();
		c2.name = "Hi Hi";
		c2.age = 11;
		System.out.println(c2.nation);// CHN
		Chinese.sleep();
		System.out.println(c1.toString());
	}
}

class Chinese {
	String name;
	int age;
	static String nation;

	public void eat() {
		System.out.println("Chinese eat Chinese food");
	}

	public static void sleep() {
		System.out.println("Chinese Sleep in Chinakang");
		// eat(); Report errors
		System.out.println(Chinese.nation);
		walk();
	}

	public static void walk() {
	}

}

2. Single Case Mode

1. Understanding the singleton pattern

         The so-called singleton design mode of a class is to take certain methods to ensure that there is only one object instance for a class in the whole software system, and that the class only provides a way to get its object instance. If we want to make a class produce only one object in a virtual machine, we must first set the access rights of the class's constructor to private, so that, You can't generate objects of a class outside the class with the new operator, but you can still generate objects of that class inside the class. Since an object of the class cannot be obtained from outside the class, only a static method of the class can be called to return the object created inside the class, and a static method can only access static member variables in the class, so variables that point to the object of the class generated inside the class must also be defined as static.

2. Implementation:

         The hungry-man style is when the instance is created in the direct constructor, while the lazy style is when the method is called

2.1 Hungry Han Style

public final class SingletonTest1 {
public static void main(String[] args) {
	Bank bank1 = Bank.getBank();
	Bank bank2 = Bank.getBank();
	//At this point, two banks appeared, one of which is actually the one in the calling constructor
	//Prove
	System.out.println(bank1 == bank2);//true
	
}
}

//*********************************************************1 Hungry Han Style
class Bank{
	//1. Privatization Constructor
	private Bank(){
		
	}
	//2. Create an object instance internally
	//4. Object set to static
	 private static Bank bank = new Bank();
	 
	//Provide a method to invoke this object, you can't create an instance outside, you can't invoke a method, we think of class to invoke, so use static,
	//However, static methods can only call static variables, so this bank instance is also static, so use static in the second step
	 public static Bank getBank() {
		 return bank;
	 }
	
}

2.2 Lazy

package com.haiyu.java;

public class SingletonTest2 {
	public static void main(String[] args) {
		Bank bankk1 = Bank.getBank();
		Bank bankk2 = Bank.getBank();
		
		System.out.println(bankk1 == bankk2);//true indicates that this is a bankk
	}
}

//*************************************2 Lazy
class Bankk {//There cannot be two identical classes under the same package here
	// 1. Privatization Constructor
	private Bankk() {

	}

	// 2. Declare the current class object but do not initialize it
	//Set bank to static
	private static Bankk bankk = null;
	//3. Method of declaring public and static to return object instances
	public static Bankk getBankk() {
		if(bankk == null) {
			bankk = new Bankk();
		}
		return bankk;
	}
}

Keywords: Java C++ Singleton pattern

Added by tallberg on Mon, 06 Dec 2021 05:39:22 +0200