Static keyword resolution in Java

Static static static static static static static static A: How do you do? B: How do you do? C: How do you do? The following is an outline of the table of contents:

Use of the static keyword

Static keyword error

Common written interview questions

If there is anything wrong, we hope to understand and welcome criticism and correction.

Use of the static keyword

On page P86 of Java Programming Ideas, there is this passage:

“Static methods are methods without this. You cannot call a non-static method from inside a static method, and vice versa. And you can call static methods just from the class itself without creating any objects. This is actually the main purpose of static methods.”

The static keyword is used to define the static method. The static keyword is used to define the static method. The static keyword is used to define the static method.

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

Obviously, methods or variables decorated with the static keyword do not need to rely on the object to be accessed, as long as the class is loaded, it can be accessed by the class name.

Static can be used to modify class member methods, class member variables, and you can write static code blocks to optimize program performance.

1) Static methods

Static methods are called static methods. Because static methods can be accessed without any object, there is no such thing as this for static methods, because there is no object attached to them. And because of 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 depend on the specific object to be called.

Note, however, that while non-static member methods and variables cannot be accessed in static methods, static member methods/variables can be accessed in non-static member methods. Here’s a simple example:

In the above code, because the print2 method exists independently of the object, it can be called directly using the class name. If you can access non-static methods/variables from static methods, then if you have the following statement in the main method:

MyObject.print2();

There are no objects, str2 doesn’t exist at all, so there’s a contradiction. The same is true for methods. Since you cannot predict whether a non-static member variable is accessed in print1, it is also forbidden to access non-static member methods in static member methods.

For non-static member methods, access to static member methods/variables is obviously unlimited.

Therefore, if you want to call a method without creating an object, you can set the method to static. Our most common static method is the main method, and it’s now clear why the main method must be static. Because the program does not create any objects when it executes the main method, it is only accessed by the class name.

Also remember that a class’s constructor is actually a static method even if it is not explicitly declared static.

2) Static variables

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

Static member variables are initialized in the order defined.

3) Static code block

Another key use of the static keyword is to form static blocks of code to optimize program performance. Static blocks can be placed anywhere in a class, and there can be more than one static block in a class. When the class is first loaded, each static block is executed in the same order as the static block, and only once.

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

 

1 class Person{ 2 private Date birthDate; 3 4 public Person(Date birthDate) { 5 this.birthDate = birthDate; 6 } 7 8 boolean isBornBoomer() { 9 Date startDate = Date.valueOf("1946"); 10 Date endDate = Date.valueOf("1964"); 11 return birthDate.compareTo(startDate)>=0 && birthDate.compareTo(endDate) < 0; 13 12}}Copy the code

 

IsBornBoomer is used to determine whether the person was born between 1946 and 1964. Each time isBornBoomer is called, startDate and birthDate are generated, resulting in a waste of space.

 

1 class Person{ 2 private Date birthDate; 3 private static Date startDate,endDate; 4 static{ 5 startDate = Date.valueOf("1946"); 6 endDate = Date.valueOf("1964"); 7 } 8 9 public Person(Date birthDate) { 10 this.birthDate = birthDate; 11 } 12 13 boolean isBornBoomer() { 14 return birthDate.compareTo(startDate)>=0 && birthDate.compareTo(endDate) < 0; 16 15}}Copy the code

 

As a result, many times initialization operations that only need to be done once are placed in static code blocks.

Static keyword error

1. Does the static keyword change the access permissions of members of a class?

Some beginners confuse static in Java with the static keyword in C/C++. Just one thing to keep in mind here: Unlike static in C/C++, the static keyword in Java does not affect the scope of variables or methods. The only keywords that affect access rights in Java are private, public, and protected (including package access rights). Take a look at the following example:

The static keyword does not change the access to variables and methods.

2. Can static member variables be accessed through this?

Although this is not available for static methods, can static member variables be accessed through this in non-static methods? Take a look at the following example. What is the output of this code?

1 public class Main {2 static int value = 33; 3 4 public static void main(String[] args) throws Exception{ 5 new Main().printValue(); 6 } 7 8 private void printValue(){ 9 int value = 3; 10 System.out.println(this.value); 12 11}}Copy the code
33
Copy the code

This is static. What does this stand for? This represents the current object, so if you call printValue with new Main(), the current object is the object that was generated by new Main(). Static variables are owned by objects, so the value of this.value in printValue is 33. The value inside the printValue method is a local variable and cannot be associated with this at all, so the output is 33. One thing to always remember here is that static member variables are independent of the object, but that doesn’t mean they can’t be accessed through the object. All static methods and static variables can be accessed through the object (as long as the access is sufficient).

3. Can static apply to local variables?

Static can be used to scope local variables in C/C++, but in Java, remember that static is not allowed to modify local variables. Don’t ask why, it’s a rule of Java syntax.

Common written interview questions

Static static static static static static static static static

1. What is the output of this code?

1 public class Test extends Base{ 2 3 static{ 4 System.out.println("test static"); 5 } 6 7 public Test(){ 8 System.out.println("test constructor"); 9 } 10 11 public static void main(String[] args) { 12 new Test(); 13 } 14 } 15 16 class Base{ 17 18 static{ 19 System.out.println("base static"); 20 } 21 22 public Base(){ 23 System.out.println("base constructor"); 25 24}}Copy the code
base static
test static
base constructor
test constructor
Copy the code

The main method is the entry point to the program, but the Test class must be loaded before the main method is executed. When the Test class is loaded, the Test class inherits from the Base class, so the Base class is not loaded first. When the Base class is loaded, the static block is found, and the static block is executed. After the Base class is loaded, we continue to load the Test class, and when we find that the Test class also has a static block, we execute the static block. After loading the required classes, the main method is executed. Executing new Test() in the main method calls the parent class’s constructor first, and then its own constructor. Hence the output above.

2. What is the output of this code?

1 public class Test { 2 Person person = new Person("Test"); 3 static{ 4 System.out.println("test static"); 5 } 6 7 public Test() { 8 System.out.println("test constructor"); 9 } 10 11 public static void main(String[] args) { 12 new MyClass(); 13 } 14 } 15 16 class Person{ 17 static{ 18 System.out.println("person static"); 19 } 20 public Person(String str) { 21 System.out.println("person "+str); 22 } 23 } 24 25 26 class MyClass extends Test { 27 Person person = new Person("MyClass"); 28 static{ 29 System.out.println("myclass static"); 30 } 31 32 public MyClass() { 33 System.out.println("myclass constructor"); 34}} 35Copy the code
test static
myclass static
person static
person Test
test constructor
person MyClass
myclass constructor
Copy the code

Similarly, let’s think about how to execute this code. The Test class is loaded first, so the static block in the Test class is executed. New MyClass() is then executed, and the MyClass class has not yet been loaded, so it needs to be loaded. When loading MyClass, we find that MyClass inherits from Test, but since Test is already loaded, we only need to load MyClass, and then execute the static block of MyClass. After the load is complete, the object is generated through the constructor. Person Person = new Person() in Test. The Person class has not yet been loaded, so the Person class is loaded first and the Person static block is executed. The constructor of the parent class is executed, the parent class is initialized, and the parent class is initialized, so the Person Person = new Person() of MyClass is executed, and the constructor of MyClass is executed.

3. What is the output of this code?

1 public class Test { 2 3 static{ 4 System.out.println("test static 1"); 5 } 6 public static void main(String[] args) { 7 8 } 9 10 static{ 11 System.out.println("test static 2"); 13 12}}Copy the code
test static 1
test static 2
Copy the code

Although there are no statements in the main method, it still prints, for the reasons described above. In addition, static blocks can appear anywhere in the class (as long as they are not inside a method, remember, not inside any method) and are executed in the same order as static blocks.

Static:

1. Static is a modifier used to modify a member

2. Static modified members are shared by all objects

3. Static takes precedence over objects because static members already exist when the class is loaded

4. Static members can be called by the class name if they have one more method. A static variable

5. Static data is shared data, and storage in an object is unique data

Member variables differ from static variables:

1. The life cycles of the two variables are different:

Member variables exist as the object is created and are released as the object is reclaimed

Static variables exist as the class is loaded and disappear as the class disappears

2. Different call methods:

Member variables can only be called by objects;

Static variables can be called by objects as well as by class names

3. Different aliases:

Member variables are also called instance variables

Static variables are called class variables

4. Data is stored in different locations

Member variable data is stored in objects in heap memory, so it is also called object specific data

Static variable data is stored in the static section of the method section (shared data section), so it is also called shared data for objects

References:

Lavasoft.blog.51cto.com/62575/18771…

www.51cto.com/specbook/24…

Blog.csdn.net/zhu_apollo/…

Blog.sina.com.cn/s/blog_70b8…

Hi.baidu.com/yuiezt/item…

Bbs.csdn.net/topics/3302…

Yezixingchen.iteye.com/blog/159718…

Ideas for Java Programming