Pull hook education Java employment emergency training camp learning notes static keyword

Keywords: Java

Learning resources: Pull hook Education

Case Title: encapsulation and testing of People class

  • Program to realize the encapsulation of People class. The features include: name, age and nationality. It is required to provide a method to print all features
  • The PeopleTest class is programmed. The main method uses the parametric method to construct two objects and print them
/*
	Programming the encapsulation of People class
*/
public class People {
	
	// 1. Privatize the member variable and use the private keyword to modify it
	private String name;
	private int age;
	private String country;
	
	// 3. in the construction method, we call the set method to judge the reasonable value.
	public People() {}
	public People(String name, int age, String country) {
		setName(name);
		setAge(age);
		setCountry(country);
	}
	
	// 2. Provide public get and set methods, and judge the reasonable value in the method body
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		if(age > 0 && age < 150) {
			this.age = age;
		} else {
			System.out.println("Age is unreasonable!");
		}
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	
	public void show() {
		System.out.println("full name:" + getName() + " Age:" + getAge() + " Nationality:" + getCountry());
	}
}
/*
	Programming the test of People class
*/
public class PeopleTest {
	
	public static void main(String[] args) {
		
		// 1. Construct two objects with parameters and print them
		People p1 = new People("Fei Zhang",30,"China");
		p1.show();
		People p2 = new People("Guan Yu",35,"China");
		p2.show();
	}
}

Basic concepts of static keyword

  • Use the static keyword to modify the member variable to indicate the meaning of static. At this time, the member variable is promoted from the object level to the class level, that is, there is only one copy of the whole class and shared by all objects. The member variable is ready with the loading of the class, regardless of whether to create an object.
  • Members modified by static keyword can be accessed by * * reference. But it is recommended to access by class name. *
/*
	Programming the encapsulation of People class
*/
public class People {
	
	// 1. Privatize the member variable and use the private keyword to modify it
	private String name;
	private int age;
	public static String country;
	
	// 3. in the construction method, we call the set method to judge the reasonable value.
	public People() {}
	public People(String name, int age, String country) {
		setName(name);
		setAge(age);
		setCountry(country);
	}
	
	// 2. Provide public get and set methods, and judge the reasonable value in the method body
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		if(age > 0 && age < 150) {
			this.age = age;
		} else {
			System.out.println("Age is unreasonable!");
		}
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	
	public void show() {
		System.out.println("full name:" + getName() + " Age:" + getAge() + " Nationality:" + getCountry());
	}
}
/*
	Programming the test of People class
*/
public class PeopleTest {
	
	public static void main(String[] args) {
		
		// 3. Verify whether the static member (class member) modified by the static keyword has nothing to do with creating the object
		System.out.println("The obtained nationality information is:" + People.country);
		
		System.out.println("------------------------");
		// 1. Construct two objects with parameters and print them
		People p1 = new People("Fei Zhang",30,"China");
		p1.show();
		People p2 = new People("Guan Yu",35,"China");
		p2.show();
		
		System.out.println("------------------------");
		// 2. Verify whether the static member (class member) modified by the static keyword is shared by all objects
		p1.country = "Shu Kingdom";
		System.out.println("The nationality of the first object is" + p1.country);
		System.out.println("The nationality of the second object is" + p2.country);
		People p3 = new People();
		System.out.println("The nationality of the third object is" + p3.country);
	}
}

How to use the static keyword

  • In the non static member method, you can access both non static and static members. (static members are shared by all objects)
  • In a static member method, only static members can be accessed, and non static members cannot be accessed. (because the object may not have been created at this time)
  • In future development, only the content belonging to the class level and shared by all objects can be modified with the static keyword. (static keyword cannot be abused)
/*
	Programming the use of static keyword
*/
public class StaticTest {
	
	private int cnt = 1;	// It belongs to the object level, that is, each object has an independent share
	private static int snt = 2;	// Belonging to the class level, that is, all objects share the same copy
	
	// Custom non static member methods 	 Access by reference is required
	public void show() {
		System.out.println("cnt = " + this.cnt);	// 1
		System.out.println("snt = " + this.snt);	// two 	 Static members are shared by all objects, and the this keyword can be omitted
	}
	
	// Custom static member methods. It is recommended to use the class name
	public static void test() {
		//System.out.println("cnt = " + this.cnt); 	//  one 	 There is no this keyword in the static member method, because it can be called in the way of class name
		System.out.println("snt = " + this.snt);	// 2
	}
	
	public static void main(String[] args) {
		StaticTest st = new StaticTest();
		st.show();
		
		System.out.println("----------------------");
		StaticTest.test();
	}
}

Building blocks and static code blocks (familiar)

  • Building block: a code block directly enclosed by {} in the class body. The building block will be executed every time an object is created
  • Static code block: a construction block decorated with the static keyword. The static code block will be executed in turn with the loading of the class
/*
	Programming to achieve the use of building blocks and static code blocks
*/
public class BlockTest {
	
	// When you need to do some preparatory work before executing the constructor body, you can write the relevant code of the preparatory work in the constructor block
	// For example, agree to initialize a member variable
	{
		System.out.println("+++Tectonic block");
	}
	
	// Static code blocks are ready as the class loads, and execute faster than the construct
	// When you need to do some preparatory work with the class loading before executing the code block, write the code into the static code block
	// For example, load the driver package of the database, etc
	static {
		System.out.println("===Static code block");
	}
	
	// Custom construction method
	public BlockTest() {
		System.out.println("---Construction method body");
	}
	
	public static void main(String[] args) {
		
		BlockTest bt = new BlockTest();
		BlockTest bt2 = new BlockTest();
	}
	
}

See also main method

  • Syntax format public static void main(String[] args)
  • Examples of parameter usage
/*
	Test of programming main method
*/
public class MainTest {
	
	public static void main(String[] args) {
		
		System.out.println("The number of elements in the parameter array is:" + args.length);
		System.out.println("Pass to main The actual parameters of the method are:");
		for(int i = 0 ; i < args.length ; i++) {
			System.out.println("Subscript is" + i + "The value of the formal parameter variable is:" + args[i]);
		}
	}
}

Case Title: encapsulation and testing of Singleton class (top priority)

  • Programming the encapsulation of Singleton class
  • The Singleton class is programmed to test the Singleton class. It is required that only one object of this class can be obtained in the main method
/*
	Programming the encapsulation of Singleton class (hungry Chinese style)
*/
public class Singleton {
	
	// 2. Declare that the reference of this class type points to the object of this class type 	 Use the private static keyword to decorate together
	private static Singleton sin = new Singleton();	
	
	// 1. The privatization construction method is modified with the private keyword
	private Singleton() {}
	
	// 3. Provide a public get method to return the object 	 Use the public static keyword to decorate together
	public static Singleton getInstance() {
		return sin;
	}
}
/*
	Programming Singleton class encapsulation (lazy)
*/
public class Singleton {
	
	// 2. Declare that the reference of this class type points to the object of this class type 	 Use the private static keyword to decorate together
	private static Singleton sin = null;
	
	// 1. The privatization construction method is modified with the private keyword
	private Singleton() {}
	
	// 3. Provide a public get method to return the object 	 Use the public static keyword to decorate together
	public static Singleton getInstance() {
		if(null == sin) {
			sin = new Singleton();
		}
		return sin;
	}
}
/*
	Test of programming Singleton class
*/
public class SingletonTest {
	
	public static void main(String[] args) {
		
		// 1. Declare that the reference of Singleton type points to the object of this type
		//Singleton s1 = new Singleton();
		//Singleton s2 = new Singleton();
		//System.out.println(s1 == s2); 	//  Compare whether the value of variable S1 is equal to the value of variable S2. false
		//Singleton.sin = null; 		 You can invalidate a reference variable
		Singleton s1 = Singleton.getInstance();
		Singleton s2 = Singleton.getInstance();
		System.out.println(s1 == s2);	// true
	}
}

Singleton design pattern

  • In some special cases, when a class provides only one object externally, such a class is called a singleton class, and the process and idea of designing a singleton are called a singleton design pattern.
  • Implementation flow of singleton design pattern
    • The privatization construction method is modified with the private keyword
    • Declare that the reference of this class type points to the object of this class type, and use the private static keyword to modify it
    • Provide a public get method, which is responsible for returning the object, and use the public static keyword to modify it
  • Implementation of singleton design pattern
    • Two types: hungry man type and lazy man type. Hungry man type is recommended in future development

Posted by groundwar on Mon, 22 Nov 2021 14:24:01 -0800