this and static

Posted by GBahle on Mon, 28 Feb 2022 06:58:01 +0100

static:
1. Static is translated as "static"
2. All static keywords are class related and class level
3. All static modifications adopt "class name." Access by
4. Static modified variable: static variable
5. Static modification method: static method

Classification of variables:
Variables are divided according to the declared location:
The variables declared in the method body are called local variables
Variables declared outside the method are called member variables

Member variables can be divided into:
Instance variable
Static variable

class VarTest{
	// The following examples are all object related, and "reference" is used when accessing The new object must be accessed first
	// Related to strength, you must have an object before you can access it. Null pointer exceptions may occur
	// Instance variables in member variables
	int i;
	// Example method
	public void m1(){		
	}
	
	// Static variables in member variables
	static int k;
	// Static method
	public static void m2(){
		
	}
}

When are variables declared instance and static?
If a property value of all objects of this type is the same,
It is not recommended to define as instance variable, which wastes memory space. It is recommended to define as
Class level features are defined as static variables, and only one is reserved in the method area
Copies, saving memory overhead.

An object is an instance variable
One copy of multiple objects is a static variable

public class staticTest02
{
	public static void main(String[] args){
		 Chinese c1 =new Chinese("123456","junker","China");
		 Chinese c2 =new Chinese("00000","jun","China");
		 System.out.println(c1.id);
		 System.out.println(c1.name);
		 System.out.println(c1.country);
		 System.out.println(c2.id);
		 System.out.println(c2.name);
		 System.out.println(c2.country);		
}
}
// Define a class: Chinese

class Chinese{
	// ID number
	// Each person's ID number is different, so the ID number should be an instance variable, and an object is a copy.
	String id;
	// full name
	// The name is also a person's name, and the name should also be an instance variable
	String name;
	// nationality
	// For the "Chinese", the nationality is "China" and will not change with the change of the object
	// Obviously, nationality is not a characteristic of the object level
	// Nationality is characteristic of the whole class
	String country;
	
	// No parameters
	public Chinese(){
		
	}

	// With parameters
	public Chinese(String s1,String s2,String s3){
		id =s1;
		name =s2;
		country =s3;
	}
}

When the variable country is not static, the memory diagram is as follows:

When the variable country is static, the code walkthrough and memory diagram are as follows:

Code walkthrough 2: (for static static variables, you should use "class name." (accessed by)

public class staticTest02
{
	public static void main(String[] args){
		
		// Visiting Chinese nationality
		// Nationality is a static variable, and "class name" should be used Access by
		 Chinese c1 =new Chinese("123456","junker");
		 System.out.println(Chinese.country);
		 
		 // Error [id,name is an instance variable. You should first new an object through "reference." [access by]
		 //System,out.println(Chinese.id);
		 
		 System.out.println(c1.id);
		 System.out.println(c1.name);
		 
		 Chinese c2 =new Chinese("00000","jun");
		 System.out.println(c2.id);	
		 System.out.println(c2.name);
		 System.out.println(Chinese.country);
		 		
}
}
// Define a class: Chinese

class Chinese{
	// ID number
	// Each person's ID number is different, so the ID number should be an instance variable, and an object is a copy.
	String id;
	// full name
	// The name is also a person's name, and the name should also be an instance variable
	String name;
	// nationality
	// Key five stars: variables with static are called static variables
	// Static variables are initialized when the class is loaded. There is no need for a new object, and the space of static variables is opened
	// Static variables are stored in the method area
	// Static variables usually give a value
	static String country ="China";
	
	// No parameters
	public Chinese(){
		
	}

	// With parameters
	public Chinese(String s1,String s2){
		id =s1;
		name =s2;
	}
}

Code drill 3:

Example: you must use "reference." To visit
Static: it is recommended to use "class name." To access, but use "reference." You can also, if you use "reference." To access will confuse others: what do programmers think is an instance

public class staticTest02
{
	public static void main(String[] args){
		 Chinese c1 =new Chinese("123456","junker");
		 
		 // Error [id,name is an instance variable. You should first new an object through "reference." [access by]
		 //System,out.println(Chinese.id);
		 
		 System.out.println(c1.id);
		 System.out.println(c1.name);
	 // Visiting Chinese nationality
	// Nationality is a static variable, and "class name" should be used Access by
		 System.out.println(Chinese.country);
		 
		 Chinese c2 =new Chinese("00000","jun");
		
		 System.out.println(c2.id);	
		 System.out.println(c2.name);
		 
		 // Use reference The way of access is still through 
		 System.out.println(c2.country);	// China
		 		
}
}
// Define a class: Chinese

class Chinese{
	String id;
	
	String name;

	static String country ="China";
	
	// No parameters
	public Chinese(){
		
	}

	// With parameters
	public Chinese(String s1,String s2){
		id =s1;
		name =s2;
	}
}

Null reference accessing static pointer exception

public class staticTest02
{
	public static void main(String[] args){
		 Chinese c1 =new Chinese("123456","junker");
		 System.out.println(c1.id);
		 System.out.println(c1.name);
		 System.out.println(Chinese.country);
		 
		 Chinese c2 =new Chinese("00000","jun");	
		 System.out.println(c2.id);	
		 System.out.println(c2.name);
		 
		 // c2 is an empty reference
		 c2 =null;

		 // No null pointer exception will occur
		 // Because static variables do not require the existence of objects
		 // In fact, when the following code runs, it is still: system out. println(Chinese.country);
		 System.out.println(c2.country);// Still pass	

		 // A null pointer exception occurred because name is an instance variable
		 // System.out.println(c2.name);
		 		
}
}
// Define a class: Chinese

class Chinese{
	String id;
	
	String name;

	static String country ="China";
	
	// No parameters
	public Chinese(){
		
	}

	// With parameters
	public Chinese(String s1,String s2){
		id =s1;
		name =s2;
	}
}

Supplement [Master]

public class staticTest03
{
	public static void main(String[] args){
		User u =new User();
		int i =u.getId();
		System.out.println(i);
	}
}


class User
{	
	// Instance variables are also called object variables
	private int id; // Variable with id at object level
	
	/*
	// Will report an error
	// Static methods cannot be used because the returned id and id itself are object level, and a new object is required
	// Static methods only need "class name." So an error will be reported
	public static int getId(){
		return id;
	}
	*/
	public int getId(){
		return id;
	}

}

When a static method is used, the variable should also be static:

1, Static code block

1. Use the static keyword to define static code blocks

2. What are static code blocks and syntax?
       static{
java statement;
        ...
       }

3. When will static code blocks be executed?
Features: it is executed when the class is loaded, and only once
    
4. When the main method is loaded and executed, note: before the main method is executed

5. Static code blocks are generally executed from top to bottom

6. What are the functions and uses of static code blocks?

Specific business:
The project manager said: in all the programs we write, as long as the class is loaded, please record the log information of class loading
(in which year, month, day, hour and minute, which class is loaded into the JVM) you need to use static code blocks

Code drill:

public class staticTest04
{	
	// Static code block
	static{
		System.out.println("A");
	}
	// Multiple static code blocks can be written in a class
	static{
		System.out.println("B");
	}
	static{
		System.out.println("C");
	}

	// Program entry
	public static void main(String[] args){
		System.out.println("come on~");
		}

	// Then write a static code block
	static{
		System.out.println("D");
	}
}

Running result: (conclusion: static code block is executed when class is loaded)

2, Code execution sequence

(conclusion: static code block 1 and static code block 2 have sequence, and static code block and static variable also have sequence)

Code demonstration:

public class staticTest05
{	
	// When are static variables initialized? 	 Class loading
	// Where are static variables stored? 	 Method area
	static int i =100;
	
	// When do static code blocks execute? 	 Class loading
	static{
		System.out.println("i:"+i);
	}
	
	/*
	Instance variable k variable is an instance variable, and the memory space will be opened up only when the construction method (hidden object method) is executed (new time)
	Error: cannot reference non static variable k from static context
	int k =6;
	static{
		// static Static code blocks are executed during class loading, and k is an instance variable, so an error will be reported
		System.out.println(k);

		System.out.println("name:"+name); // An error is also reported here: illegal forward reference
	}
	

	// Error reporting: illegal forward reference
	//static{System.out.println("name:"+name);}
	//static String name ="junker";
	
	*/
	//Program entry
	public static void main(String[] args){
		System.out.println("main begin~");
	}	
}

3, Memory structure of this

1. this is a keyword, all lowercase
    
2. What is this and how is it in terms of memory?
One object, one this.
this is a variable and a reference. this saves the memory address of the current object and points to itself,
Therefore, strictly speaking, this represents the "current object"
         
this is stored inside the object in heap memory
    
3. This can only be used in instance methods. Whoever calls this instance method is who this is
So this represents the current object
    
4. this can be omitted in most cases

5. Why can't this be used in static methods???
this represents the current object. There is no object in the static method

Code drill: [Master]

public class ThisTest01
{	
	public static void main(String[] args){
	// new an object
	Customer c1 =new Customer("junker");
	Customer c2 =new Customer("jun");
	c1.shopping();
	c2.shopping();
	
	}
}




// Customer category
class Customer
{
	// attribute
	// Instance variable (must use "reference") (accessed by)
	String name;

	// Construction method
	public Customer(){
		
	}
	public Customer(String s){
		name =s;
	}

	// Customer shopping methods
	// Example method
	public void shopping(){
		// Who is this here? This is the current object
		// c1 calls shopping();,this is c1
		// c2 calls shopping();,this is c2
		System.out.println(name+"Shopping");
		// Question: String name is an instance variable (must use "reference." (accessed by)
		// Why (name + "shopping"); Isn't it (quoting. Name + "shopping") 
		// Why not in this program (c1.name + "shopping"); And?
		// [Note: the reference c1 from new is in the body of ThisTest01 method, which is in the body of Customer class];

		// Answer: actually (name + "shopping"); The current object this is hidden (see the memory diagram for details)
		//System.out.println(this.name + "shopping");
	}
}

Extension:

/*
	Analysis: can i variable be accessed in main method???
*/

public class ThisTest02{	

	int i =100;	// The instance variable is at the object level. You must use the new object first
	static String k; 
	public static void main(String[] args){
		// Error: cannot reference non static from static context
		//System.out.println(this.i);
		//System.out.println(i); 	//  Similarly, an error is reported because I is an instance variable and the main method is static
		
		// If you really want to access instance variables in the main method, you need to manually new an object
		ThisTest02 t =new ThisTest02();
		System.out.println(t.i);
		// Access the static variable "class name." Access by
		System.out.println(ThisTest02.k);
	}
}

Topics: Java