Java keywords -- static, final

static keyword

Static keyword, expressed as "static and global". Resources (variables or methods) modified by static can be called directly through the class name without instantiation

1. The resources declared by static can only be initialized once, and the initialization is completed after the whole program is compiled and before running;

public class Father {
	
	public String name;
	public int age;
	
	//Static code block
	static {
		System.out.println("ststic");
	}
	
	public Father() {//Nonparametric construction method
		System.out.println("Father");
	}

	public static void main(String[] args) {
		Father f = new Father();
	}

}

ststic
Father

2 modified variables are called static variables. Local variables (such as variables in methods) cannot be modified by static, because static has global meaning;
3. Modified methods are called static methods. Static methods can only call other static resources, cannot call non static variables, and cannot apply this and super, because static resources are loaded before instantiation;

public class Father {
	
	public String name;
	public int age;
	public static String subject; //Static resources
	//Static code block
	static {
		System.out.println("ststic");
	}
	//Static method
	public static void learn() {
		//Static methods can only access static resources
		subject = "English";
	}
}

4 variables and methods modified by static are independent of any object of the class. That is, it does not depend on a specific instance of the class and is shared by all instances of the class

public class Father {
	
	public String name;
	public int age;
	public static String subject; //Static resources
	//Static code block
	static {
		System.out.println("ststic");
	}
	public static void learn() {
		//Static methods can only access static resources
		subject = "English";
	}
	public static void main(String[] args) {
		Father f = new Father();
		f.learn();// √
		Father.learn();// √
		new Father().learn();// √
	}

}

5. Modify the internal class (static class). The external class does not need to be instantiated and can be called directly through the external class name

public class Father {

	//Inner class
	class Mother{

	}
	
	public static void main(String[] args) {
		Father f = new Father();
		Mother m = f.new Mother();
	}
	
}
public class Father {
	
	//Inner class
	static class Mother{

	}
	
	public static void main(String[] args) {
		Mother m = new Father.Mother();
	}
	
}
  • supplement
    The instance method is bound to the instance to which it belongs
    The static method is bound to the class it belongs to
    Static code blocks are executed in order before the main method
    Note:
    Static code blocks cannot exist in any method body
    Static code blocks cannot directly access instance variables and instance methods.

What is the loading order of the following codes?

public class Father {
	//Static code block
	static {
		System.out.println("Father-ststic");
	}
	//Construction method
	public Father() {
		System.out.println("Father");
	}
}

public class Child extends Father{
	//Static code block
	static {
		System.out.println("Child-ststic");
	}
	//Construction method
	public Child() {
		System.out.println("Child");
	}
	public static void main(String[] args) {
		Child c = new Child();
	}
}

Father-ststic
Child-ststic
Father
Child

Class loading order of JVM
Static declared static resources > New > {code block} > constructor
In child parent classes: parent static resources > child static resources > parent code block > parent construction method > child code block > child construction method

public class Father {
	//Static code block
	static {
		System.out.println("Father-ststic");
	}
	//Code block
	{
		System.out.println("Father-codeblock");
	}
	//Construction method
	public Father() {
		System.out.println("Father");
	}
}

public class Child extends Father{
	//Static code block
	static {
		System.out.println("Child-ststic");
	}
	//Code block
	{
		System.out.println("Child-codeblock");
	}
	//Construction method
	public Child() {
		System.out.println("Child");
	}
	
	public static void main(String[] args) {
		Child c = new Child();
	}
	
}

Father-ststic
Child-ststic
Father-codeblock
Father
Child-codeblock
Child

final keyword

Final keyword, expressed as "final and unchangeable", is used to modify classes, methods and variables.

1. The class modified by final cannot be inherited;
2. Modification methods cannot be rewritten;
3. The modified variable is a constant, the constant name must be capitalized, and the constant must be assigned a value;
4 modify the basic data type, and the value of the variable cannot be changed;
5. Modify the reference type (object). The reference (memory address) of the object cannot be changed, that is, it cannot point to another object after initialization.

final int NO = 12;
final String NAME = "abc";

Keywords: Java Back-end

Added by jarcoal on Tue, 04 Jan 2022 19:13:05 +0200