This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging

Java-static keyword: java-static keyword: java-static

  • Java static Static code block
  • Java static static method

Java static Static code block

The syntax for a static block of code looks like this:

Class {// static code block static{Java statement; }}Copy the code

Static code blocks are executed when the class is loaded, and only once. It’s not used much in development, but sometimes you can’t write code without it.

Static code blocks are actually a special time in the Java language for programmers. This time is called class loading time. If you want to execute a piece of code at class loading time, that piece of code is targeted.

For example, if you want to parse a file during class loading and require that the file be parsed only once, you can write the code that parsed the file into a static code block.

Let’s test a static block of code:

Public class StaticTest01 {static{system.out.println (2); } static{system.out.println (1); } public static void main(String[] args) {system.out.println ("main execute!") ); } static{system.out.println (0); }}Copy the code

The running result is as shown in the figure below:

Figure 1: Static code block running results

Through the above test that you can write a class of multiple static blocks of code (although in most cases only write one), and follow the top-down static blocks of code in the order, so some time in the class body of code is executed order (in most cases the class body of code order requirements, The code in the method body is sequential (the code in the method body must be executed line by line in a top-down order), and the code in the static block is executed before the main method is executed, because the static block is executed only once when the class is loaded.

Look at the following code again:

public class StaticTest02 { int i = 100; static{ System.out.println(i); }}Copy the code

The compilation results are shown in the figure below:

Figure 2: A compilation error for accessing an instance variable in a static code block

Why compile error?

That’s because the I variable is an instance variable, and the instance variable must be accessed by creating an object first. The static code block is executed when the class loads, and the object hasn’t been created yet, so the I variable can’t be accessed in this way. You can consider adding static before I to make it a static variable. You don’t need to create an object to access the static variable. You can access it directly from the “class”.

public class StaticTest02 { static int i = 100; Static {system.out.println (" I = "+ I); } public static void main(String[] args) { } }Copy the code

The running result is as shown in the figure below:

Figure 3: Accessing static variables in a static code block

How about code like this?

Public class StaticTest02 {static{system.out.println (" I = "+ I); } static int i = 100; }Copy the code

Compiler error, please look at the following:

Figure 4: Compile error message

Through the test, you can see sometimes the class body of code with order requirements (class defines two independent methods of body, the two methods are not order required), static blocks of code execution when class loading, static variables are initialized when class loading, they happen at the same time, so there will always be the order of the requirements, If you want to access the I variable in a static block of code, the I variable must be placed before the static block.

Java static static method

Under what circumstances are methods declared static? Method actually describes the actions, I think that when an action at the time of trigger which require the participation of object, the method should be defined as instance methods, for example: each person playing basketball will play basketball, but you play basketball and kobe Bryant playing basketball final effect is not the same, obviously play basketball this movement exists object differentiation, this method should be defined as instance methods.

Or: every high school students have a test, examination and students with excellent grades but you the end result is not the same, on a “house university”, a “tsinghua university”, apparently also take object to participate in this action, so this method should be defined as instance methods exam.

Described above is from the perspective of design thought to choose, but also can be to determine, from the perspective of the code in the method body need direct access to the current object instance variables and instance methods, this method must be defined as instance methods, because only in the instance methods have this, the static method does not exist in this.

Look at the code:

public class Customer { String name; public Customer(String name){ this.name = name; } public void shopping(){// access the current object name system.out.println (name + "shopping!"); ); // Continue to make the current object pay(); } public void pay(){system.out.println (name + "paying!"); ); }}Copy the code
public class CustomerTest { public static void main(String[] args) { Customer jack = new Customer("jack"); jack.shopping(); Customer rose = new Customer("rose"); rose.shopping(); }}Copy the code

The running result is as shown in the figure below:

Figure 5: Run results

In the above code, different customer shopping, the final effect is different, in addition to the shopping() method to access the current object instance variable name, and call the instance method pay(), so shopping() method cannot be defined as static method, must be declared as an instance method.

In addition, in the actual development, “tool class” in the method is generally defined as static method, because the tool class is to facilitate everyone’s use, the method is defined as static method, more convenient to call, do not need to create an object, direct use of the class name can access.

Look at the following utility classes to simplify “system.out.println ();” Utility classes written in code:

public class U { public static void p(int data){ System.out.println(data); } public static void p(long data){ System.out.println(data); } public static void p(float data){ System.out.println(data); } public static void p(double data){ System.out.println(data); } public static void p(boolean data){ System.out.println(data); } public static void p(char data){ System.out.println(data); } public static void p(String data){ System.out.println(data); }}Copy the code

The running result is as shown in the figure below:

Figure 6: Test tool class