The three major functions of the static keyword:
(Recommended tutorial: java introductory tutorial)
static static member variable
static static member method
static static code block
Analysis:
1. static static member variable
Explanation: If a If the member variable uses the static keyword, then the variable no longer belongs to the object itself, but to the class it belongs to. Multiple objects share the same data.
Code example:
We build a Student class and set two static member variables: room classroom and idCounter. Instantiate two student objects one and two in the main() method, and only assign a value to the room of the one object. When printing, you will find that the value of two.room is the same as the value of one.room.
It can be seen that because room uses the static keyword, it belongs to the class and no longer belongs to the object itself, and can be shared by multiple objects. Because idCounter is a static variable and will only be initialized once, the ID of each object created will increase by 1.
public class Demo01StaticField { public static void main(String[] args) { Student one=new Student("郭靖",19); Student two=new Student("黄蓉",16); one.room="101教室"; System.out.println("姓名:"+one.getName()+",年龄:" +one.getAge()+",教室:"+one.room +",学号:"+one.getId()); //姓名:郭靖,年龄:19,教室:101教室,学号:1 System.out.println("姓名:"+two.getName() +",年龄:"+two.getAge()+",教室:"+two.room +",学号:"+two.getId()); //姓名:黄蓉,年龄:16,教室:101教室,学号:2 } } public class Student { private int id; //学号 private String name; //姓名 private int age; //年龄 static String room; //所在教室 private static int idCounter=0; //学号计数器,每当new了一个新对象的时候,计数器++ public Student() { this.id= ++idCounter; } public Student(String name, int age) { this.name = name; this.age = age; this.id= ++idCounter; } public int getId() { return id; } public void setId(int id) { this.id = id; } //name和age的Getter,Setter方法同上id,省略 }
2. static static member method
Explanation: If a member method uses the static keyword, similarly, then this becomes a static method. Static methods do not belong to objects, but to classes.
Advantages of static modified member methods: It avoids the cumbersomeness and resource consumption of new objects and can be used directly through [class name. method name].
Code examples:
Create a new Myclass class, which has one member variable, one static member variable, one member method, and one static member method. We can see that member methods can access both member variables and static variables. Static methods can only access static variables, cannot access non-static variables, and cannot use the this keyword. When using this class, with the static keyword, you don't need to create an object, you can use it directly through the class name. For static methods in this class, the class name can be omitted.
public class Demo02StaticMethod { public static void main(String[] args) { //非静态方法使用:1.首先创建对象 MyClass obj=new MyClass(); //2.然后才能使用没有static关键字的方法 obj.method(); //对于静态方法来说,可以通过对象名进行调用,也可以通过类名称来调用。 obj.methodStatic(); //正确,不推荐,这种写法也会被javac翻译成“类名称.静态方法名” MyClass.methodStatic(); //正确,推荐 //对于本类当中的静态方法,可以省略类名称 myMethod(); Demo02StaticMethod.myMethod(); //完全等效 } public static void myMethod(){ System.out.println("自己的方法!"); } } public class MyClass { int num; //成员变量 static int numStatic; //静态变量 //成员方法 public void method(){ System.out.println("这是一个普通的成员方法。"); //成员方法可以访问成员变量 System.out.println(num); //成员方法可以访问静态变量 System.out.println(numStatic); } //静态方法 public static void methodStatic(){ System.out.println("这是一个普通的静态方法。"); //静态方法可以访问静态变量 System.out.println(numStatic); //静态不能直接访问非静态【重点】 //System.out.println(num); //错误写法 //静态方法中不能使用this关键字 //System.out.println(this); //错误写法 } }
(Learning video recommendation: java course)
3. static static code block
Format:
public class 类名称{ static{ //静态代码块的内容 } }
Features: When this class is used for the first time, the static code block is executed only once, which can be used to optimize program performance. In most cases, we will put some initialization operations (initializing static resources) code that will only be performed once in the static code block, such as loading configuration files, etc. Note: Static content always takes precedence over non-static, so static code blocks are executed before constructors.
public class Demo04StaticCode { public static void main(String[] args) { Person one=new Person(); System.out.println("************************"); //无论创建几个Person对象,静态代码块只执行一次 Person two=new Person(); } } public class Person { static{ System.out.println("静态代码块执行!"); } public Person() { System.out.println("构造方法执行!"); } }
Execution result:
The above is the detailed content of Three major functions of the static keyword. For more information, please follow other related articles on the PHP Chinese website!