Java - 12 static variables and code blocks

I. Static

static is a keyword used to modify member variables and member methods

static features:

(1) Can be shared by all objects

(2) Calls can be made using class names

(3) Static loading takes precedence over objects

(4) Loading as classes are loaded

For example, we write a code to define a student class, which has three member variables: name, age, school, and then a member method. To create two student objects, their names and ages are different, but the school is the same. If there are thousands of objects of the same school, we have to rewrite a lot of code. In order to improve the efficiency of the code, we define the same school member variable as a static variable.

public class StaticDemo {
	public static void main(String[] args) {
		Student.school = "tsinghua";
		
		Student s1 = new Student();
		s1.name = "Zhang San";
		s1.age = 18;
		//s1.school = Tsinghua;
		s1.studyEnglish();
		
		Student s2 = new Student();
		s2.name = "Li Si";
		s2.age = 28;
		s2.studyEnglish();
		
	}

}


class Student{
	String name;
	int age;
	static String school;
	
	public void studyEnglish(){
		System.out.println(name+"Learn English well");
	}
}

Matters needing attention

Static method:

Static member variables can be invoked, and static member methods can be invoked.

Non-static member variables cannot be invoked, and non-static member methods cannot be invoked.

That is, static methods can only call static members

Non-static method:

Static member variables can be invoked, and static member methods can be invoked.

Non-static member variables can be invoked, and non-static member methods can be invoked.

There is no this object in the static method, because what, static loading takes precedence over the object, it loads with the loading of the class, and this is the current object that calls this method, there is no object to this.

I'll write a piece of code to confirm the above.

public class StaticDemo {
	public static void main(String[] args) {
		Student.school = "tsinghua";
		//Student.sleep();
		Student.song();
			
	}

}


class Student{
	String name;
	int age;
	static String school;
	
	public static void sleep(){
		System.out.println(school);
		//System.out.println(age); error-prone
	}
	
	public static void song(){
		sleep();
		//studyEnglish(); will report errors
	}
	
		
	public void studyEnglish(){
		System.out.println(name+"Learn English well");
		sleep();
		System.out.println(school);
		System.out.println(name);
	}
}

Advantages and disadvantages of static state

A: Static advantages:

The shared data of objects can be stored in separate space, which saves space. It is not necessary to store one copy of each object. It can be invoked directly by class name instead of creating objects in heap memory. Static members can access members directly by class name, which is more convenient than creating objects.

B: Static drawbacks:

Visits have limitations. (Static is good, but it can only be accessed)

Static usage

For example, our mathematical calculation, Math

Class variables and instance variables

A: Class variables: Actually, static variables

Define Location: Define Outside of Method in Class

Located memory area: method area

Lifecycle: Loading as classes are loaded

Feature: No matter how many objects are created, class variables are only in the method area and have only one copy.

B: Instance variables: Actually, they are non-static variables.

Define Location: Define Outside of Method in Class

Memory area: heap

Lifecycle: Loaded as objects are created

Feature: For every object created, there is an instance variable in the heap

2. Code Block

Local code blocks: defined in methods or statements

public class BlockDemo {
	public static void main(String[] args) {
		{
			for(int i=0;i<10;i++){
				System.out.println("I love Java");
			}
		}

	}

}

Constructing blocks: blocks of code that define the location of members in a class

In this way, both parametric and non-parametric constructions will be output.

public class Teacher {
	String name;
	int age;
	
	{
		for(int x = 0;x <2;x++) {
			System.out.println("I love Java");
		}
		System.out.println("I love Java");
	} 
	

	public Teacher() {
		System.out.println("I am an empty structure.");
	}
	
	public Teacher(String name,int age) {
		System.out.println("I have parametric structure.");
		
		this.name = name;
		this.age = age;
	}
	
	
}

Static code blocks: code blocks defined at member locations and modified with static

Static Code Block: Loaded as the class is loaded, only once. Some initialization is needed when loading the class, such as load driver.

public class Teacher {
	String name;
	int age;
	
	static{
		 System.out.println("I love Java");
	} 
	

	public Teacher() {
		System.out.println("I am an empty structure.");
	}
	
	public Teacher(String name,int age) {
		System.out.println("I have parametric structure.");
		
		this.name = name;
		this.age = age;
	}
	
	
}

Execution order of each code block

Construct code blocks:

Priority to constructor execution, constructor blocks are used to perform initialization actions required by all objects

Each creation of an object executes a block of construction code.

Static code block:

It takes precedence over the execution of the main method, over the execution of the construction code block, when it is first used in any form.

No matter how many objects are created in this class, static code blocks are executed only once.

It can be used to assign static variables and initialize classes.

For instance

Write out the execution order of statements

public class BlockTest {
	static {
		System.out.println("BlockTest Static code block execution");
	}
	
	{
		System.out.println("BlockTest Construct code block execution");
	}
	

	public BlockTest(){
		System.out.println("BlockTest The parametric construct is executed");
	}
	
	public static void main(String[] args) {
		System.out.println("BlockTest The main function is executed.");
		Coder c = new Coder();
		Coder c2 = new Coder();
	}
}

class Coder {
	
	static {
		System.out.println("Coder Static code block execution");
	}
	
	{
		System.out.println("Coder Construct code block execution");
	}
	
	public Coder() {
		System.out.println("Coder Nonparametric construction execution");
	}	
	
}

BlockTest Static Code Block Execution - - BlockTest Main Function Executed - - Coder Static Code Block Execution - - Coder Construction Code Block Execution - - Coder Construction Code Block Execution - - Coder Construction Code Block Execution - - Coder Non-Parametric Construction Execution

Keywords: Java

Added by fourlincoln10 on Fri, 26 Jul 2019 17:24:20 +0300