Three major functions of the static keyword
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In-depth analysis of the role and usage of the static keyword in C language. In C language, static is a very important keyword, which can be used in the definition of functions, variables and data types. Using the static keyword can change the link attributes, scope and life cycle of the object. Let’s analyze the role and usage of the static keyword in C language in detail. Static variables and functions: Variables defined using the static keyword inside a function are called static variables, which have a global life cycle

Title: Is go a keyword in C language? Detailed analysis In C language, "go" is not a keyword. Keywords in C language are specified by the C standard and are used to represent specific grammatical structures or functions. They have special meanings in the compiler and cannot be used as identifiers or variable names. For example, the keyword "int" represents an integer data type, "if" represents a conditional statement, and so on. If we want to verify whether "go" is a keyword in C language, we can write a simple program to test it. Here is an example: #inc

The role and examples of var keyword in PHP In PHP, the var keyword is used to declare a variable. In previous PHP versions, using the var keyword was the idiomatic way to declare member variables, but its use is no longer recommended. However, in some cases, the var keyword is still used. The var keyword is mainly used to declare a local variable, and the variable will automatically be marked as local scope. This means that the variable is only visible within the current block of code and cannot be accessed in other functions or blocks of code. Use var

The role and usage of static in C language: 1. Variable scope; 2. Life cycle; 3. Internal function; 4. Modify global variables; 5. Modify function; 6. Other uses; Detailed introduction: 1. Variable scope, when If there is the static keyword before a variable, then the scope of the variable is limited to the file in which it is declared. In other words, the variable is a "file-level scope", which is very useful for preventing the "duplicate definition" problem of variables; 2. Life cycle, static variables are initialized once when the program starts executing, and destroyed when the program ends, etc.

There are 32 keywords in C language. According to the function of keywords, they can be divided into four categories: data type keywords, control statement keywords, storage type keywords and other keywords. There are 12 data type keywords, including char, double, float, int, etc.; there are 12 control statement keywords, including for, break, if, else, do, etc.; there are 4 storage type keywords, including auto, static , extern, etc.; there are 4 other keywords, including const, sizeof, etc.

In the Go language, while is not a keyword. You can use the for statement plus break to achieve the effect of a while loop, such as "for {sum++ if sum>10{break}else{...}}". The go language has 25 keywords such as break, default, func, select, case, defer, go, map, else, goto, for, if, var, etc.

1. static Please look at the following program first: publicclassHello{publicstaticvoidmain(String[]args){//(1)System.out.println("Hello, world!");//(2)}} Have seen this Segment programs are familiar to most people who have studied Java. Even if you have not learned Java but have learned other high-level languages, such as C, you should be able to understand the meaning of this code. It simply outputs "Hello, world" and has no other use. However, it shows the main purpose of the static keyword.

Practical application scenarios and usage skills of the static keyword in C language 1. Overview static is a keyword in C language, used to modify variables and functions. Its function is to change its life cycle and visibility during program running, making variables and functions static. This article will introduce the actual application scenarios and usage techniques of the static keyword, and illustrate it through specific code examples. 2. Static variables extend the life cycle of variables. Using the static keyword to modify local variables can extend their life cycle.
