static keyword in Java

1. Purpose of static keyword

There is this passage on page P86 of “Java Programming Thoughts”:

“A static method is a method without this. Non-static methods cannot be called inside a static method, and the reverse is possible. And the static method can be called only through the class itself without creating any objects. This is actually This is the main purpose of the static method.”

Although this passage only explains the special features of the static method, we can see the basic function of the static keyword. In short, it can be described in one sentence:

Convenient to call (method/variable) without creating an object.

Obviously, methods or variables modified by the static keyword do not need to rely on objects for access. As long as the class is loaded, it can be accessed through the class name.

Static can be used to modify class member methods and class member variables. In addition, static code blocks can be written to optimize program performance.

1) static method

Static methods are generally called static methods. Since static methods can be accessed without relying on any object, there is no this for static methods because they are not attached to any objects. Since there are no objects, there is no this. Not to mention this. And due to this feature, non-static member variables and non-static member methods of the class cannot be accessed in static methods, because non-static member methods/variables must rely on specific objects before they can be called.

But it should be noted that although non-static member methods and non-static member variables cannot be accessed in static methods, static member methods/variables can be accessed in non-static member methods. Give a simple example:

2) static variable

Static variables are also called static variables. The difference between static variables and non-static variables is that static variables are shared by all objects and have only one copy in memory. It will be initialized when and only when the class is first loaded. . Non-static variables are owned by objects and are initialized when the object is created. There are multiple copies, and the copies owned by each object do not affect each other.

?The initialization order of static member variables is initialized in the order in which they are defined.

3) static code block

The static keyword also plays a key role in forming static code blocks to optimize program performance. A static block can be placed anywhere in a class, and there can be multiple static blocks in a class. When a class is loaded for the first time, each static block will be executed in the order of the static blocks, and will only be executed once.

Why the static block can be used to optimize program performance is because of its characteristics: it will only be executed once when the class is loaded. Here’s an example:

public class person {
private Date birthday;
    
public person() {

}
public person(Date birthday) {
this.birthday = birthday;
}

public String isBornBoomer() {
Date startBirthday = java.sql.Date.valueOf("1946-1-1");
Date endBirthday = java.sql.Date.valueOf("1964-12-31");
return (birthday.compareTo(startBirthday) >= 0 & amp; & amp; birthday.compareTo(endBirthday) < 0) ? "Conforms" : "Does not conform";
}

public static void main(String[] args) {
Date bb = java.sql.Date.valueOf("1999-12-21");
person p = new person(bb);
System.out.println(p.isBornBoomer());
}

}

isBornBoomer is used to determine whether the person was born between 1946 and 1964. Every time isBornBoomer is called, two objects, startDate and birthDate, will be generated, resulting in a waste of space. It will be more efficient if it is changed to this:

import java.util.Date;
/**
 * static keyword, static code block: Put some initialization operations that only need to be performed once in the static code block.
 *
 *@authoradmin
 *
 */
public class person {
private Date birthday;
private static Date startBirthday, endBirthday;

static {
startBirthday = java.sql.Date.valueOf("1946-1-1");
endBirthday = java.sql.Date.valueOf("1964-12-31");
}

public person() {

}

public person(Date birthday) {
this.birthday = birthday;
}

public String isBornBoomer() {
//isBornBoomer is used to determine whether the person was born between 1946 and 1964. Every time isBornBoomer is called,
//Both objects startDate and birthDate will be generated, causing a waste of space.
// Date startBirthday = java.sql.Date.valueOf("1946-1-1");
// Date endBirthday = java.sql.Date.valueOf("1964-12-31");
return (birthday.compareTo(startBirthday) >= 0 & amp; & amp; birthday.compareTo(endBirthday) < 0) ? "Conforms" : "Does not conform";
}

public static void main(String[] args) {
Date bb = java.sql.Date.valueOf("1999-12-21");
person p = new person(bb);
System.out.println(p.isBornBoomer());
}

}

Therefore, many initialization operations that only need to be performed once are performed in static code blocks.

2. Misunderstandings about the static keyword

1. Will the static keyword change the access permissions of members in the class?

The static keyword in Java does not affect the scope of variables or methods. In Java, the only keywords that can affect access rights are private, public, and protected (including package access rights). Look at the following example to understand:

2. Can static member variables be accessed through this?

Although there is no this for static methods, can static member variables be accessed through this in non-static methods? Let’s look at the following example first. What is the output result of this code?

public class TestMain4 {
static int value = 33;
public static void main(String[] args) {
new TestMain4().printValue();
}
\t
private void printValue() {
int value = 3;
System.out.println(this.value);
}

}

Here we mainly examine the understanding of this and static. What does this represent? this represents the current object, so if printValue is called through new Main(), the current object is the object generated through new Main(). The static variable is owned by the object, so the value of this.value in printValue is definitely 33. The value inside the printValue method is a local variable and cannot be associated with this at all, so the output result is 33. Always remember one thing here: although static member variables are independent of objects, it does not mean that they cannot be accessed through objects. All static methods and static variables can be accessed through objects (as long as the access rights are sufficient).

3. Can static act on local variables?

Remember in Java: static is not allowed to modify local variables. Don’t ask why, this is a requirement of Java grammar.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Java skill treeBasic syntax of JavaKeywords in Java 138147 people are learning the system