1 static static variable
1.1 Use of static variables
static variables are also called static variables, also called class variables
Static variables are shared by all objects and have only one copy in memory
If and only if the class is initialized when it is first loaded
Static variables belong to class
Static variables can be called directly through the class name
It can also be called by object name.static variable name
static variable
public class Student{ private String name; private static String schoolName; private static int count; public Student(String name){ this.name = name; count++; } public void showStuInfo(){ System.out.println("Student’s name is " + this.name + ", the name of the school is " + Student.schoolName); } public static void main(String[] args){ Student.schoolName = "Experimental Middle School"; Student s1 = new Student("Zhang San "); Student s2 = new Student("李思"); Student s3 = new Student("王五"); s1.showStuInfo(); s2.showStuInfo(); s3.showStuInfo(); System.out.println("The number of students is: " + Student.count); System.out.println("The number of students is: " + s1.count); } }
1.2 The difference between instance variables and static variables
- Static variables belong to a class, which does not produce objects. Static variables can be called through the class name;
Instance variables belong to objects of this class, and an object of this class must be generated before instance variables can be called.
- Static variables exist in the method area as the class is loaded;
Instance variables exist in the heap memory as the object is created.
- Static variables are created at the beginning of the program and destroyed at the end of the program;
Instance variables are created when the object is created and destroyed when the object is destroyed.
2 static methods
A method modified by static is called a static method, also called a class method.
In static methods, non-static member variables and non-static member methods cannot be directly accessed.
In static methods, the this keyword cannot be used
Static methods can be called directly through the class name
It can also be called by object name.static method name
Static methods and static method access
public class Student{ private String name; private int age; private int studentId; private static String classRoom; public static void showClassRoom(){ //Silent method used this 关键字 System.out.println("Classroom 3301"); } public static void main(String[] args){ Student.showClassRoom(); } }
3 static blocks
3.1 Use of static blocks
Static code blocks are executed when the class is loaded and only executed once
The this keyword cannot be included in static code blocks
There can be multiple static blocks, which are executed in order
public class Emp{ static{ System.out.println("Welcome"); } static{ System.out.println("Come again"); } }
3.2 Static blocks, anonymous construction blocks, constructors
Execution order
public class Son{ static{ System.out.println("static block 1"); } private String name; public Son(String name){ this.name = name; System.out.println("Constructor"); } { System.out.println("Anonymous construction block"); } static{ System.out.println("static block 3"); } public static void main(String[] args){ Son s1 = new Son("张三"); Son s2 = new Son("李四"); Son s3 = new Son("王五"); } }
The output is as follows:
static block 1
static block 3
anonymous building blocks
Construction method
anonymous building blocks
Construction method
anonymous building blocks
Construction method
The order of execution is: static block --> anonymous block --> constructor method
4 static introduction
If we need to frequently reference methods under a certain class, such as related data calculation classes under java.lang.Math
the class , or org.junit.Assert
various assertion methods under the class used for unit testing, we can use static references. This saves the need to repeatedly write the previous class name.
import static java.lang.Math.random; import static org.junit.Assert.assertTrue; public class test { public static void main(String[] args) { double random = random(); //Method in Math System. out.println(random); assertTrue(random > 0.3); //Method in Assert new test().normalMethod(); } //Static introduced method, also used in ordinary methods public void normalMethod(){ double random = random(); System.out.println(random); assertTrue(random > 0.7); } }
Methods introduced statically can be used in both ordinary methods and static methods.