Introduction to Java (15) - static

Introduction to Java (15) - static

Table of contents

Introduction to Java (15) - static

premise

static

Use the static keyword to define "static code blocks"

Example statement block/code block [less used]

Sample code


premise

 * When member variables are declared as instance variables:
      - All objects have this attribute, but the value of this attribute will change as the object changes
 * When member variables are declared as static variables:
      - All objects have this attribute, and The median value of this attribute of all objects is the same. It is recommended to define it as a static variable to save memory overhead
 - static variables are initialized when the class is loaded, and the memory is allocated in the method area. There is no need to create an object when accessing, directly use the "class name. static variable" method to access
 
 * When is the method defined as static:
      - The method describes an action. Only when everyone's actions are consistent, then this action can be promoted For class-level actions
 - instance variables and instance methods cannot be directly accessed in static methods.
 - Most methods are defined as instance methods.
 - Most of the methods in "tool classes" are static methods, which are convenient for programming and calling

static

       - Static means static
       - The modified method is a static method
       - The modified variable is a static variable
       - Therefore, static modified elements can be accessed using the "class name." method, and of course they can also be accessed using the "reference." method. [But not recommended]
       - All modified elements are class-level features and have nothing to do with specific objects.

Use the static keyword to define "static code blocks"

      * Syntax format:
           static{                java statement;            }       - The static code block is executed when the class is loaded, and the value is executed once. No need to call it in the main method.       - You can write multiple ones in one class, and follow the top-down Sun Xu to execute them in sequence       * Function:            - Relevant to specific needs. eg: The project requires that the code execution completion log be recorded at the time/timing of class loading.            - Java prepares a special moment for programmers, called: class loading time.       - Usually the preparation work is completed in the static code block, and the data preparation tool is completed first. eg: Initialize the connection pool, parse the XML configuration file...







Example statement block/code block [less used]

     - You can also write multiple, executed in top-down order
      - executed before the constructor method. The constructor methods are executed sequentially, and the instance code blocks are executed once.
      - The instance code block is also a special opportunity prepared by the Java language for programmers, called: object initialization opportunity

Sample code


class People{//JVM结构见static.jpg
	String name;
	int age;
	//上面两个变量需要随对象改变,设置为实例变量
	static String id="中国公民";//身份
	//这个属性不需要改变,设置为静态变量,存储在方法区内存当中
	
	public People(String name,int age) {
		this.name=name;
		this.age=age;
	}
}

public class Static {
	static {
		System.out.println("这里是static定义的“静态代码块”;");
	}
	static {
		System.out.println("这里是static定义的“静态代码块”。");
	}
	static {
		System.out.println("-----------------------------------------");
	}
	
	public Static(){
		System.out.println("--------这里“无参构造方法”---------");
	}
	
	{
		System.out.println("这里“实例代码块”;");
	}
	{
		System.out.println("这里“实例代码块”。");
	}
	
	public static void main(String[] agrs) {
		Static s1=new Static();
		Static s2=new Static();
		System.out.println("------------------------------------------");
		
		People p1=new People("张三",24);
		System.out.println(p1.name+","+p1.age+","+People.id);//id静态变量可以直接用类调用
		People p2=new People("李四",36);
		System.out.println(p2.name+","+p2.age+","+People.id);
		p2=null;
		System.out.println(p2.id);//即便采用引用的方式调用,对象指向空,也不受影响出现空指针异常
		System.out.println("------------------------------------------");
	}

}

Guess you like

Origin blog.csdn.net/qq_61562251/article/details/135046691